@@ -5,31 +5,116 @@ import (
55 "fmt"
66 "os"
77
8- "k8s.io/client-go/kubernetes "
8+ "k8s.io/client-go/rest "
99 "k8s.io/client-go/tools/clientcmd"
10+
11+ configclient "github.com/openshift/client-go/config/clientset/versioned"
12+ configclientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
13+ configv1alpha1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1alpha1"
14+ configv1alpha2 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1alpha2"
1015)
1116
12- // getKubeClient creates a Kubernetes clientset.
13- func getKubeClient () (* kubernetes. Clientset , error ) {
17+ // getKubeConfig get KUBECONFIG file from environment variable
18+ func getKubeConfig () (* rest. Config , error ) {
1419 configPath , present := os .LookupEnv ("KUBECONFIG" )
20+ fmt .Print ("configPath: " , configPath )
1521 if ! present {
1622 return nil , errors .New ("the environment variable KUBECONFIG must be set" )
1723 }
1824 config , err := clientcmd .BuildConfigFromFlags ("" , configPath )
25+ return config , err
26+ }
27+
28+ // getClient creates a configclient.Clientset instance.
29+ func getClient () (* configclient.Clientset , error ) {
30+ config , err := getKubeConfig ()
31+ if err != nil {
32+ return nil , fmt .Errorf ("unable to load build config: %w" , err )
33+ }
34+ // Create the Clientset
35+ clientset , err := configclient .NewForConfig (config )
36+ if err != nil {
37+ return nil , fmt .Errorf ("unable to create a Kubernetes clientset: %w" , err )
38+ }
39+
40+ return clientset , nil
41+ }
42+
43+ // getV1Client creates a configclientv1.ConfigV1Client instance.
44+ func getV1Client () (* configclientv1.ConfigV1Client , error ) {
45+ config , err := getKubeConfig ()
1946 if err != nil {
2047 return nil , fmt .Errorf ("unable to load build config: %w" , err )
2148 }
2249 // Create the Clientset
23- clientset , err := kubernetes .NewForConfig (config )
50+ clientset , err := configclientv1 .NewForConfig (config )
2451 if err != nil {
2552 return nil , fmt .Errorf ("unable to create a Kubernetes clientset: %w" , err )
2653 }
54+
55+ return clientset , nil
56+ }
57+
58+ // getV1alpha1Client creates a configv1alpha1.ConfigV1alpha1Client instance.
59+ func getV1alpha1Client () (* configv1alpha1.ConfigV1alpha1Client , error ) {
60+ // Create the Clientset
61+ config , err := getKubeConfig ()
62+ if err != nil {
63+ return nil , fmt .Errorf ("unable to load build config: %w" , err )
64+ }
65+ clientset , err := configv1alpha1 .NewForConfig (config )
66+ if err != nil {
67+ return nil , fmt .Errorf ("unable to create a Kubernetes clientset: %w" , err )
68+ }
69+
2770 return clientset , nil
2871}
2972
30- // MustGetKubeClient creates a Kubernetes clientset, or panics on failures.
31- func MustGetKubeClient () * kubernetes.Clientset {
32- clientset , err := getKubeClient ()
73+ // getV1alpha2Client creates a configv1alpha2.ConfigV1alpha2Client instance.
74+ func getV1alpha2Client () (* configv1alpha2.ConfigV1alpha2Client , error ) {
75+ // Create the Clientset
76+ config , err := getKubeConfig ()
77+ if err != nil {
78+ return nil , fmt .Errorf ("unable to load build config: %w" , err )
79+ }
80+ clientset , err := configv1alpha2 .NewForConfig (config )
81+ if err != nil {
82+ return nil , fmt .Errorf ("unable to create a Kubernetes clientset: %w" , err )
83+ }
84+
85+ return clientset , nil
86+ }
87+
88+ // MustGetClient creates a configclient.Clientset instance, or panics on failures.
89+ func MustGetClient () * configclient.Clientset {
90+ clientset , err := getClient ()
91+ if err != nil {
92+ panic ("unable to create a Kubernetes clientset: " + err .Error ())
93+ }
94+ return clientset
95+ }
96+
97+ // MustGetV1Client creates a configclientv1.ConfigV1Client instance, or panics on failures.
98+ func MustGetV1Client () * configclientv1.ConfigV1Client {
99+ clientset , err := getV1Client ()
100+ if err != nil {
101+ panic ("unable to create a Kubernetes clientset: " + err .Error ())
102+ }
103+ return clientset
104+ }
105+
106+ // MUSTGetV1alpha1Client creates a configv1alpha1.ConfigV1alpha1Client instance, or panics on failures.
107+ func MUSTGetV1alpha1Client () * configv1alpha1.ConfigV1alpha1Client {
108+ clientset , err := getV1alpha1Client ()
109+ if err != nil {
110+ panic ("unable to create a Kubernetes clientset: " + err .Error ())
111+ }
112+ return clientset
113+ }
114+
115+ // MUSTGetV1alpha2Client creates a configv1alpha2.ConfigV1alpha2Client instance, or panics on failures.
116+ func MUSTGetV1alpha2Client () * configv1alpha2.ConfigV1alpha2Client {
117+ clientset , err := getV1alpha2Client ()
33118 if err != nil {
34119 panic ("unable to create a Kubernetes clientset: " + err .Error ())
35120 }
0 commit comments