|
| 1 | +package clients |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + autopilotv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot/v1beta2" |
| 7 | + k0shelmv1beta1 "github.com/k0sproject/k0s/pkg/apis/helm/v1beta1" |
| 8 | + k0sv1beta1 "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1" |
| 9 | + embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1" |
| 10 | + velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" |
| 11 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 14 | + coreclientset "k8s.io/client-go/kubernetes/scheme" |
| 15 | + "k8s.io/client-go/metadata" |
| 16 | + "k8s.io/client-go/rest" |
| 17 | + "k8s.io/client-go/tools/clientcmd" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | +) |
| 20 | + |
| 21 | +var localSchemeBuilder = runtime.NewSchemeBuilder( |
| 22 | + apiextensionsv1.AddToScheme, |
| 23 | + embeddedclusterv1beta1.AddToScheme, |
| 24 | + //nolint:staticcheck // SA1019 we are using the deprecated scheme for backwards compatibility, we can remove this once we stop supporting k0s v1.30 |
| 25 | + autopilotv1beta2.AddToScheme, |
| 26 | + //nolint:staticcheck // SA1019 we are using the deprecated scheme for backwards compatibility, we can remove this once we stop supporting k0s v1.30 |
| 27 | + k0sv1beta1.AddToScheme, |
| 28 | + //nolint:staticcheck // SA1019 we are using the deprecated scheme for backwards compatibility, we can remove this once we stop supporting k0s v1.30 |
| 29 | + k0shelmv1beta1.AddToScheme, |
| 30 | + velerov1.AddToScheme, |
| 31 | +) |
| 32 | + |
| 33 | +type KubeClientOptions struct { |
| 34 | + RESTClientGetter genericclioptions.RESTClientGetter |
| 35 | + KubeConfigPath string |
| 36 | +} |
| 37 | + |
| 38 | +func getScheme() *runtime.Scheme { |
| 39 | + s := runtime.NewScheme() |
| 40 | + coreclientset.AddToScheme(s) |
| 41 | + localSchemeBuilder.AddToScheme(s) |
| 42 | + return s |
| 43 | +} |
| 44 | + |
| 45 | +// NewKubeClient returns a new kubernetes client. |
| 46 | +func NewKubeClient(opts KubeClientOptions) (client.Client, error) { |
| 47 | + var restConfig *rest.Config |
| 48 | + if opts.RESTClientGetter == nil && opts.KubeConfigPath == "" { |
| 49 | + return nil, fmt.Errorf("a valid kube config is required to create a kube client") |
| 50 | + } |
| 51 | + |
| 52 | + if opts.RESTClientGetter == nil { |
| 53 | + conf, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfigPath) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("unable to process kubernetes config for kube client: %w", err) |
| 56 | + } |
| 57 | + restConfig = conf |
| 58 | + } else { |
| 59 | + conf, err := opts.RESTClientGetter.ToRESTConfig() |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("unable to process rest client config for kube client: %w", err) |
| 62 | + } |
| 63 | + restConfig = conf |
| 64 | + } |
| 65 | + |
| 66 | + return client.New(restConfig, client.Options{Scheme: getScheme()}) |
| 67 | +} |
| 68 | + |
| 69 | +// NewMetadataClient returns a new kube metadata client. |
| 70 | +func NewMetadataClient(opts KubeClientOptions) (metadata.Interface, error) { |
| 71 | + var restConfig *rest.Config |
| 72 | + if opts.RESTClientGetter == nil && opts.KubeConfigPath == "" { |
| 73 | + return nil, fmt.Errorf("a valid kube config is required to create a kube client") |
| 74 | + } |
| 75 | + |
| 76 | + if opts.RESTClientGetter == nil { |
| 77 | + conf, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfigPath) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("unable to process kubernetes config for kube client: %w", err) |
| 80 | + } |
| 81 | + restConfig = conf |
| 82 | + } else { |
| 83 | + conf, err := opts.RESTClientGetter.ToRESTConfig() |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("unable to process rest client config for kube client: %w", err) |
| 86 | + } |
| 87 | + restConfig = conf |
| 88 | + } |
| 89 | + |
| 90 | + return metadata.NewForConfig(restConfig) |
| 91 | +} |
0 commit comments