diff --git a/pkg/agent/config.go b/pkg/agent/config.go index 06474a19..ed72afe4 100644 --- a/pkg/agent/config.go +++ b/pkg/agent/config.go @@ -19,7 +19,8 @@ import ( "github.com/jetstack/preflight/api" "github.com/jetstack/preflight/pkg/client" "github.com/jetstack/preflight/pkg/datagatherer" - "github.com/jetstack/preflight/pkg/datagatherer/k8s" + "github.com/jetstack/preflight/pkg/datagatherer/k8sdiscovery" + "github.com/jetstack/preflight/pkg/datagatherer/k8sdynamic" "github.com/jetstack/preflight/pkg/datagatherer/local" "github.com/jetstack/preflight/pkg/kubeconfig" "github.com/jetstack/preflight/pkg/logs" @@ -895,11 +896,11 @@ func (dg *DataGatherer) UnmarshalYAML(unmarshal func(any) error) error { switch dg.Kind { case "k8s": - cfg = &k8s.ConfigDynamic{} + cfg = &k8sdynamic.ConfigDynamic{} case "k8s-dynamic": - cfg = &k8s.ConfigDynamic{} + cfg = &k8sdynamic.ConfigDynamic{} case "k8s-discovery": - cfg = &k8s.ConfigDiscovery{} + cfg = &k8sdiscovery.ConfigDiscovery{} case "local": cfg = &local.Config{} // dummy dataGatherer is just used for testing diff --git a/pkg/agent/run.go b/pkg/agent/run.go index 6ce40a6a..609f5c4d 100644 --- a/pkg/agent/run.go +++ b/pkg/agent/run.go @@ -33,7 +33,7 @@ import ( "github.com/jetstack/preflight/api" "github.com/jetstack/preflight/pkg/client" "github.com/jetstack/preflight/pkg/datagatherer" - "github.com/jetstack/preflight/pkg/datagatherer/k8s" + "github.com/jetstack/preflight/pkg/datagatherer/k8sdynamic" "github.com/jetstack/preflight/pkg/kubeconfig" "github.com/jetstack/preflight/pkg/logs" "github.com/jetstack/preflight/pkg/version" @@ -176,7 +176,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) { return fmt.Errorf("failed to instantiate %q data gatherer %q: %v", kind, dgConfig.Name, err) } - dynDg, isDynamicGatherer := newDg.(*k8s.DataGathererDynamic) + dynDg, isDynamicGatherer := newDg.(*k8sdynamic.DataGathererDynamic) if isDynamicGatherer { dynDg.ExcludeAnnotKeys = config.ExcludeAnnotationKeysRegex dynDg.ExcludeLabelKeys = config.ExcludeLabelKeysRegex @@ -222,7 +222,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) { // the run. if err := dg.WaitForCacheSync(bootCtx); err != nil { // log sync failure, this might recover in future - if errors.Is(err, k8s.ErrCacheSyncTimeout) { + if errors.Is(err, k8sdynamic.ErrCacheSyncTimeout) { timedoutDGs = append(timedoutDGs, dgConfig.Name) } else { log.V(logs.Info).Info("Failed to sync cache for datagatherer", "kind", dgConfig.Kind, "name", dgConfig.Name, "error", err) diff --git a/pkg/datagatherer/k8s/discovery.go b/pkg/datagatherer/k8sdiscovery/discovery.go similarity index 93% rename from pkg/datagatherer/k8s/discovery.go rename to pkg/datagatherer/k8sdiscovery/discovery.go index d28b5eda..e4ce54b5 100644 --- a/pkg/datagatherer/k8s/discovery.go +++ b/pkg/datagatherer/k8sdiscovery/discovery.go @@ -1,4 +1,4 @@ -package k8s +package k8sdiscovery import ( "context" @@ -9,6 +9,7 @@ import ( "github.com/jetstack/preflight/api" "github.com/jetstack/preflight/pkg/datagatherer" + "github.com/jetstack/preflight/pkg/kubeconfig" ) // ConfigDiscovery contains the configuration for the k8s-discovery data-gatherer @@ -38,11 +39,11 @@ func (c *ConfigDiscovery) UnmarshalYAML(unmarshal func(any) error) error { // The UID is assumed to be stable for the lifetime of the cluster. // - https://github.com/kubernetes/kubernetes/issues/77487#issuecomment-489786023 func (c *ConfigDiscovery) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) { - cl, err := NewDiscoveryClient(c.KubeConfigPath) + cl, err := kubeconfig.NewDiscoveryClient(c.KubeConfigPath) if err != nil { return nil, err } - cs, err := NewClientSet(c.KubeConfigPath) + cs, err := kubeconfig.NewClientSet(c.KubeConfigPath) if err != nil { return nil, fmt.Errorf("while creating new clientset: %s", err) } diff --git a/pkg/datagatherer/k8s/cache.go b/pkg/datagatherer/k8sdynamic/cache.go similarity index 99% rename from pkg/datagatherer/k8s/cache.go rename to pkg/datagatherer/k8sdynamic/cache.go index 4482f512..99f33677 100644 --- a/pkg/datagatherer/k8s/cache.go +++ b/pkg/datagatherer/k8sdynamic/cache.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic import ( "fmt" diff --git a/pkg/datagatherer/k8s/cache_test.go b/pkg/datagatherer/k8sdynamic/cache_test.go similarity index 99% rename from pkg/datagatherer/k8s/cache_test.go rename to pkg/datagatherer/k8sdynamic/cache_test.go index f0cb5071..b3e7a5b5 100644 --- a/pkg/datagatherer/k8s/cache_test.go +++ b/pkg/datagatherer/k8sdynamic/cache_test.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic import ( "testing" diff --git a/pkg/datagatherer/k8s/dynamic.go b/pkg/datagatherer/k8sdynamic/dynamic.go similarity index 99% rename from pkg/datagatherer/k8s/dynamic.go rename to pkg/datagatherer/k8sdynamic/dynamic.go index 92f10c33..7a6349be 100644 --- a/pkg/datagatherer/k8s/dynamic.go +++ b/pkg/datagatherer/k8sdynamic/dynamic.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic // The venafi-kubernetes-agent has a requirement that **all** resources should // be uploaded, even short-lived secrets, which are created and deleted @@ -61,6 +61,7 @@ import ( "github.com/jetstack/preflight/api" "github.com/jetstack/preflight/pkg/datagatherer" + "github.com/jetstack/preflight/pkg/kubeconfig" "github.com/jetstack/preflight/pkg/logs" ) @@ -179,14 +180,14 @@ var kubernetesNativeResources = map[schema.GroupVersionResource]sharedInformerFu // NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided func (c *ConfigDynamic) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) { if isNativeResource(c.GroupVersionResource) { - clientset, err := NewClientSet(c.KubeConfigPath) + clientset, err := kubeconfig.NewClientSet(c.KubeConfigPath) if err != nil { return nil, err } return c.newDataGathererWithClient(ctx, nil, clientset) } else { - cl, err := NewDynamicClient(c.KubeConfigPath) + cl, err := kubeconfig.NewDynamicClient(c.KubeConfigPath) if err != nil { return nil, err } diff --git a/pkg/datagatherer/k8s/dynamic_test.go b/pkg/datagatherer/k8sdynamic/dynamic_test.go similarity index 99% rename from pkg/datagatherer/k8s/dynamic_test.go rename to pkg/datagatherer/k8sdynamic/dynamic_test.go index 26b6ae90..5745f254 100644 --- a/pkg/datagatherer/k8s/dynamic_test.go +++ b/pkg/datagatherer/k8sdynamic/dynamic_test.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic import ( "encoding/json" diff --git a/pkg/datagatherer/k8s/fieldfilter.go b/pkg/datagatherer/k8sdynamic/fieldfilter.go similarity index 99% rename from pkg/datagatherer/k8s/fieldfilter.go rename to pkg/datagatherer/k8sdynamic/fieldfilter.go index 08470066..392c75fd 100644 --- a/pkg/datagatherer/k8s/fieldfilter.go +++ b/pkg/datagatherer/k8sdynamic/fieldfilter.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/pkg/datagatherer/k8s/fieldfilter_test.go b/pkg/datagatherer/k8sdynamic/fieldfilter_test.go similarity index 99% rename from pkg/datagatherer/k8s/fieldfilter_test.go rename to pkg/datagatherer/k8sdynamic/fieldfilter_test.go index 0cf126c3..097e735f 100644 --- a/pkg/datagatherer/k8s/fieldfilter_test.go +++ b/pkg/datagatherer/k8sdynamic/fieldfilter_test.go @@ -1,4 +1,4 @@ -package k8s +package k8sdynamic import ( "encoding/json" diff --git a/pkg/datagatherer/k8s/client.go b/pkg/kubeconfig/client.go similarity index 81% rename from pkg/datagatherer/k8s/client.go rename to pkg/kubeconfig/client.go index 39e028ad..ad2eaff7 100644 --- a/pkg/datagatherer/k8s/client.go +++ b/pkg/kubeconfig/client.go @@ -1,19 +1,16 @@ -// Package k8s provides datagatherers for different parts of the Kubernetes API. -package k8s +package kubeconfig import ( "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" - - "github.com/jetstack/preflight/pkg/kubeconfig" ) // NewDynamicClient creates a new 'dynamic' clientset using the provided kubeconfig. // If kubeconfigPath is not set/empty, it will attempt to load configuration using // the default loading rules. func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) { - cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath) + cfg, err := LoadRESTConfig(kubeconfigPath) if err != nil { return nil, err } @@ -30,7 +27,7 @@ func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) { // kubeconfig. If kubeconfigPath is not set/empty, it will attempt to load // configuration using the default loading rules. func NewDiscoveryClient(kubeconfigPath string) (*discovery.DiscoveryClient, error) { - cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath) + cfg, err := LoadRESTConfig(kubeconfigPath) if err != nil { return nil, err } @@ -47,7 +44,7 @@ func NewDiscoveryClient(kubeconfigPath string) (*discovery.DiscoveryClient, erro // If kubeconfigPath is not set/empty, it will attempt to load configuration using // the default loading rules. func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) { - cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath) + cfg, err := LoadRESTConfig(kubeconfigPath) if err != nil { return nil, err } diff --git a/pkg/datagatherer/k8s/client_test.go b/pkg/kubeconfig/client_test.go similarity index 99% rename from pkg/datagatherer/k8s/client_test.go rename to pkg/kubeconfig/client_test.go index 31dd0e95..b885e6e9 100644 --- a/pkg/datagatherer/k8s/client_test.go +++ b/pkg/kubeconfig/client_test.go @@ -1,4 +1,4 @@ -package k8s +package kubeconfig import ( "os" diff --git a/pkg/permissions/generate.go b/pkg/permissions/generate.go index 7e3ab08e..584577c6 100644 --- a/pkg/permissions/generate.go +++ b/pkg/permissions/generate.go @@ -9,7 +9,7 @@ import ( "sigs.k8s.io/yaml" "github.com/jetstack/preflight/pkg/agent" - "github.com/jetstack/preflight/pkg/datagatherer/k8s" + "github.com/jetstack/preflight/pkg/datagatherer/k8sdynamic" ) // AgentRBACManifests is a wrapper around the various RBAC structs needed to grant the agent fine-grained permissions as per its dg configs @@ -34,7 +34,7 @@ func GenerateAgentRBACManifests(dataGatherers []agent.DataGatherer) AgentRBACMan continue } - dyConfig := dg.Config.(*k8s.ConfigDynamic) + dyConfig := dg.Config.(*k8sdynamic.ConfigDynamic) metadataName := fmt.Sprintf("%s-agent-%s-reader", agentNamespace, dyConfig.GroupVersionResource.Resource) AgentRBACManifests.ClusterRoles = append(AgentRBACManifests.ClusterRoles, rbac.ClusterRole{ diff --git a/pkg/permissions/generate_test.go b/pkg/permissions/generate_test.go index 9f8cf122..51b84e11 100644 --- a/pkg/permissions/generate_test.go +++ b/pkg/permissions/generate_test.go @@ -9,7 +9,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "github.com/jetstack/preflight/pkg/agent" - "github.com/jetstack/preflight/pkg/datagatherer/k8s" + "github.com/jetstack/preflight/pkg/datagatherer/k8sdynamic" ) func TestGenerateAgentRBACManifestsString(t *testing.T) { @@ -24,7 +24,7 @@ func TestGenerateAgentRBACManifestsString(t *testing.T) { { Name: "k8s/pods", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ GroupVersionResource: schema.GroupVersionResource{ Version: "v1", Resource: "pods", @@ -66,7 +66,7 @@ subjects: { Name: "k8s/pods", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ IncludeNamespaces: []string{"foobar"}, GroupVersionResource: schema.GroupVersionResource{ Version: "v1", @@ -110,7 +110,7 @@ subjects: { Name: "k8s/pods", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ GroupVersionResource: schema.GroupVersionResource{ Version: "v1", Resource: "pods", @@ -120,7 +120,7 @@ subjects: { Name: "k8s/nodes", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ GroupVersionResource: schema.GroupVersionResource{ Version: "v1", Resource: "nodes", @@ -205,7 +205,7 @@ func TestGenerateAgentRBACManifests(t *testing.T) { { Name: "k8s/pods", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ GroupVersionResource: schema.GroupVersionResource{ Version: "v1", Resource: "pods", @@ -263,7 +263,7 @@ func TestGenerateAgentRBACManifests(t *testing.T) { { Name: "k8s/pods", Kind: "k8s-dynamic", - Config: &k8s.ConfigDynamic{ + Config: &k8sdynamic.ConfigDynamic{ GroupVersionResource: schema.GroupVersionResource{ Version: "v1", Resource: "pods",