<commit_before>// Package k8s is a facade over (super-terrible, very difficult to understand)
// client-go to provide a higher-level interface to Kubernetes, with support
// for simple, high-level APIs for watching resources (including from stable,
// long-running processes) and implementing basic controllers.
//
// It is intended to offer the same API for (nearly) every Kubernetes resource,
// including easy CRD access without codegen.
package k8s

import (
	"fmt"
	"strings"

	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"

	"k8s.io/cli-runtime/pkg/genericclioptions"

	"k8s.io/client-go/discovery"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/restmapper"

	"github.com/google/shlex"
	"github.com/pkg/errors"
	"github.com/spf13/pflag"
)

const (
	// NamespaceAll is the argument to specify on a context when you want to list or filter
	// resources across all namespaces.
	NamespaceAll = metav1.NamespaceAll
	// NamespaceNone is the argument for a context when there is no namespace.
	NamespaceNone = metav1.NamespaceNone
)

// KubeInfo holds the data required to talk to a cluster
type KubeInfo struct {
	flags       *pflag.FlagSet
	configFlags *genericclioptions.ConfigFlags
	config      *rest.Config
	namespace   string
}

// NewKubeInfo returns a useable KubeInfo, handling optional
// kubeconfig, context, and namespace.
func NewKubeInfo(configfile, context, namespace string) *KubeInfo {
	flags := pflag.NewFlagSet("KubeInfo", pflag.PanicOnError)
	result := NewKubeInfoFromFlags(flags)

	var args []string
	if configfile != "" {
		args = append(args, "--kubeconfig", configfile)
	}
	if context != "" {
		args = append(args, "--context", context)
	}
	if namespace != "" {
		args = append(args, "--namespace", namespace)
	}

	err := flags.Parse(args)
	if err != nil {
		panic(err)
	}
	return result
}

// NewKubeInfoFromFlags adds the generic kubeconfig flags to the
// provided FlagSet, and returns a *KubeInfo that configures itself
// based on those flags.
func NewKubeInfoFromFlags(flags *pflag.FlagSet) *KubeInfo {
	configFlags := genericclioptions.NewConfigFlags(false)

	// We can disable or enable flags by setting them to
	// nil/non-nil prior to calling .AddFlags().
	//
	// .Username and .Password are already disabled by default in
	// genericclioptions.NewConfigFlags().
	//
	// Unlike client-go/discovery.CachedDiscoveryClient,
	// ericchiang/k8s doesn't support caching to the filesystem,
	// so disable the '--cache-dir' flag.
	configFlags.CacheDir = nil

	configFlags.AddFlags(flags)
	return &KubeInfo{flags, configFlags, nil, ""}
}

func (info *KubeInfo) load() error {
	if info.config == nil {
		configLoader := info.configFlags.ToRawKubeConfigLoader()

		config, err := configLoader.ClientConfig()
		if err != nil {
			return errors.Errorf("Failed to get REST config: %v", err)
		}

		namespace, _, err := configLoader.Namespace()
		if err != nil {
			return errors.Errorf("Failed to get namespace: %v", err)
		}

		info.config = config
		info.namespace = namespace
	}

	return nil
}<commit_msg>add comments and re-enable caching flag per review request<commit_after>// Package k8s is a facade over (super-terrible, very difficult to understand)
// client-go to provide a higher-level interface to Kubernetes, with support
// for simple, high-level APIs for watching resources (including from stable,
// long-running processes) and implementing basic controllers.
//
// It is intended to offer the same API for (nearly) every Kubernetes resource,
// including easy CRD access without codegen.
package k8s

import (
	"fmt"
	"strings"

	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"

	"k8s.io/cli-runtime/pkg/genericclioptions"

	"k8s.io/client-go/discovery"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/restmapper"

	"github.com/google/shlex"
	"github.com/pkg/errors"
	"github.com/spf13/pflag"
)

const (
	// NamespaceAll is the argument to specify on a context when you want to list or filter
	// resources across all namespaces.
	NamespaceAll = metav1.NamespaceAll
	// NamespaceNone is the argument for a context when there is no namespace.
	NamespaceNone = metav1.NamespaceNone
)

// KubeInfo holds the data required to talk to a cluster
type KubeInfo struct {
	flags       *pflag.FlagSet
	configFlags *genericclioptions.ConfigFlags
	config      *rest.Config
	namespace   string
}

// NewKubeInfo returns a useable KubeInfo, handling optional
// kubeconfig, context, and namespace.
func NewKubeInfo(configfile, context, namespace string) *KubeInfo {
	// Because we are constructing the args for this flagset
	// below, it's ok to use pflag.PanicOnError. We should never
	// supply it with erroneous arguments.
	flags := pflag.NewFlagSet("KubeInfo", pflag.PanicOnError)
	result := NewKubeInfoFromFlags(flags)

	var args []string
	if configfile != "" {
		args = append(args, "--kubeconfig", configfile)
	}
	if context != "" {
		args = append(args, "--context", context)
	}
	if namespace != "" {
		args = append(args, "--namespace", namespace)
	}

	err := flags.Parse(args)
	if err != nil {
		// Args is constructed by us, we should never get an
		// error, so it's ok to panic.
		panic(err)
	}
	return result
}

// NewKubeInfoFromFlags adds the generic kubeconfig flags to the
// provided FlagSet, and returns a *KubeInfo that configures itself
// based on those flags.
func NewKubeInfoFromFlags(flags *pflag.FlagSet) *KubeInfo {
	configFlags := genericclioptions.NewConfigFlags(false)

	// We can disable or enable flags by setting them to
	// nil/non-nil prior to calling .AddFlags().
	//
	// .Username and .Password are already disabled by default in
	// genericclioptions.NewConfigFlags().

	configFlags.AddFlags(flags)
	return &KubeInfo{flags, configFlags, nil, ""}
}

func (info *KubeInfo) load() error {
	if info.config == nil {
		configLoader := info.configFlags.ToRawKubeConfigLoader()

		config, err := configLoader.ClientConfig()
		if err != nil {
			return errors.Errorf("Failed to get REST config: %v", err)
		}

		namespace, _, err := configLoader.Namespace()
		if err != nil {
			return errors.Errorf("Failed to get namespace: %v", err)
		}

		info.config = config
		info.namespace = namespace
	}

	return nil
}
{'fraction_non_alphanumeric': 0.11105874960728872, 'fraction_numerical': 0.0051837888784165885, 'mean_word_length': 3.8603053435114503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '609594', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2034, 'n_words': 629}