Skip to content

Commit 717291f

Browse files
committed
Added check to see if CSI CRDs are installed before running controller
1 parent 75f9a23 commit 717291f

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

internal/pkg/cmd/reloader.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ func startReloader(cmd *cobra.Command, args []string) {
179179

180180
var controllers []*controller.Controller
181181
for k := range kube.ResourceMap {
182-
if k == "secretproviderclasspodstatuses" && !options.EnableCSIIntegration {
183-
continue
182+
if k == "secretproviderclasspodstatuses" {
183+
if !options.EnableCSIIntegration {
184+
continue
185+
}
186+
if !kube.IsCSIInstalled {
187+
logrus.Infof("Can't run CSI controller as CSI CRDs are not installed")
188+
continue
189+
}
184190
}
185191

186192
if ignoredResourcesList.Contains(k) || (len(namespaceLabelSelector) == 0 && k == "namespaces") {

pkg/kube/client.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Clients struct {
2525
var (
2626
// IsOpenshift is true if environment is Openshift, it is false if environment is Kubernetes
2727
IsOpenshift = isOpenshift()
28+
// IsCSIEnabled is true if environment has CSI provider installed, otherwise false
29+
IsCSIInstalled = isCSIInstalled()
2830
)
2931

3032
// GetClients returns a `Clients` object containing both openshift and kubernetes clients with an openshift identifier
@@ -52,9 +54,11 @@ func GetClients() Clients {
5254

5355
var csiClient *csiclient.Clientset
5456

55-
csiClient, err = GetCSIClient()
56-
if err != nil {
57-
logrus.Warnf("Unable to create CSI client error = %v", err)
57+
if IsCSIInstalled {
58+
csiClient, err = GetCSIClient()
59+
if err != nil {
60+
logrus.Warnf("Unable to create CSI client error = %v", err)
61+
}
5862
}
5963

6064
return Clients{
@@ -73,6 +77,20 @@ func GetArgoRolloutClient() (*argorollout.Clientset, error) {
7377
return argorollout.NewForConfig(config)
7478
}
7579

80+
func isCSIInstalled() bool {
81+
client, err := GetKubernetesClient()
82+
if err != nil {
83+
logrus.Fatalf("Unable to create Kubernetes client error = %v", err)
84+
}
85+
_, err = client.RESTClient().Get().AbsPath("/apis/secrets-store.csi.x-k8s.io/v1").Do(context.TODO()).Raw()
86+
if err == nil {
87+
logrus.Info("CSI provider is installed")
88+
return true
89+
}
90+
logrus.Info("CSI provider is not installed")
91+
return false
92+
}
93+
7694
func GetCSIClient() (*csiclient.Clientset, error) {
7795
config, err := getConfig()
7896
if err != nil {

0 commit comments

Comments
 (0)