Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdiscovery

import (
"context"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdynamic

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdynamic

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdynamic

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdynamic

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package k8sdynamic

import (
"encoding/json"
Expand Down
11 changes: 4 additions & 7 deletions pkg/datagatherer/k8s/client.go → pkg/kubeconfig/client.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package kubeconfig

import (
"os"
Expand Down
4 changes: 2 additions & 2 deletions pkg/permissions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down
14 changes: 7 additions & 7 deletions pkg/permissions/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -110,7 +110,7 @@ subjects:
{
Name: "k8s/pods",
Kind: "k8s-dynamic",
Config: &k8s.ConfigDynamic{
Config: &k8sdynamic.ConfigDynamic{
GroupVersionResource: schema.GroupVersionResource{
Version: "v1",
Resource: "pods",
Expand All @@ -120,7 +120,7 @@ subjects:
{
Name: "k8s/nodes",
Kind: "k8s-dynamic",
Config: &k8s.ConfigDynamic{
Config: &k8sdynamic.ConfigDynamic{
GroupVersionResource: schema.GroupVersionResource{
Version: "v1",
Resource: "nodes",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down