Skip to content

Commit 7838d84

Browse files
authored
Merge pull request #746 from GunaKKIBM/Mount-secret
Mounting secret - reading credentials from file
2 parents 90f8d4d + d57e884 commit 7838d84

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

deploy/kubernetes/base/controller.yaml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ spec:
4141
valueFrom:
4242
fieldRef:
4343
fieldPath: spec.nodeName
44-
- name: IBMCLOUD_API_KEY
45-
valueFrom:
46-
secretKeyRef:
47-
name: ibm-secret
48-
key: IBMCLOUD_API_KEY
49-
optional: true
44+
- name: API_KEY_PATH
45+
value: /etc/secrets/IBMCLOUD_API_KEY
5046
volumeMounts:
47+
- name: ibm-secret
48+
mountPath: /etc/secrets
49+
readOnly: true
5150
- name: socket-dir
5251
mountPath: /var/lib/csi/sockets/pluginproxy/
5352
ports:
@@ -65,6 +64,9 @@ spec:
6564
- name: node-update-controller
6665
image: registry.k8s.io/cloud-provider-ibm/ibm-powervs-block-csi-driver:main
6766
command: ["/node-update-controller"]
67+
env:
68+
- name: API_KEY_PATH
69+
value: /etc/secrets/IBMCLOUD_API_KEY
6870
ports:
6971
- name: metrics
7072
containerPort: 8081
@@ -80,13 +82,10 @@ spec:
8082
initialDelaySeconds: 5
8183
timeoutSeconds: 10
8284
periodSeconds: 30
83-
env:
84-
- name: IBMCLOUD_API_KEY
85-
valueFrom:
86-
secretKeyRef:
87-
name: ibm-secret
88-
key: IBMCLOUD_API_KEY
89-
optional: true
85+
volumeMounts:
86+
- name: ibm-secret
87+
mountPath: /etc/secrets
88+
readOnly: true
9089
- name: csi-provisioner
9190
image: registry.k8s.io/sig-storage/csi-provisioner:v5.0.1
9291
args:
@@ -136,3 +135,6 @@ spec:
136135
volumes:
137136
- name: socket-dir
138137
emptyDir: {}
138+
- name: ibm-secret
139+
secret:
140+
secretName: ibm-secret

deploy/kubernetes/base/node.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ spec:
4545
valueFrom:
4646
fieldRef:
4747
fieldPath: spec.nodeName
48-
- name: IBMCLOUD_API_KEY
49-
valueFrom:
50-
secretKeyRef:
51-
name: ibm-secret
52-
key: IBMCLOUD_API_KEY
48+
- name: API_KEY_PATH
49+
value: /etc/secrets/IBMCLOUD_API_KEY
5350
volumeMounts:
51+
- name: ibm-secret
52+
mountPath: /etc/secrets
53+
readOnly: true
5454
- name: kubelet-dir
5555
mountPath: /var/lib/kubelet
5656
mountPropagation: "Bidirectional"
@@ -120,3 +120,6 @@ spec:
120120
hostPath:
121121
path: /sys
122122
type: Directory
123+
- name: ibm-secret
124+
secret:
125+
secretName: ibm-secret

pkg/cloud/powervs.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
3131
"github.com/davecgh/go-spew/spew"
3232
"k8s.io/apimachinery/pkg/util/wait"
33+
"k8s.io/klog/v2"
3334
"k8s.io/utils/ptr"
3435

3536
"sigs.k8s.io/ibm-powervs-block-csi-driver/pkg/util"
@@ -60,7 +61,10 @@ func NewPowerVSCloud(cloudInstanceID, zone string, debug bool) (Cloud, error) {
6061
}
6162

6263
func newPowerVSCloud(cloudInstanceID, zone string, debug bool) (Cloud, error) {
63-
apikey := os.Getenv("IBMCLOUD_API_KEY")
64+
apikey, err := readCredentials()
65+
if err != nil {
66+
return nil, err
67+
}
6468

6569
authenticator := &core.IamAuthenticator{ApiKey: apikey, URL: os.Getenv("IBMCLOUD_IAM_API_ENDPOINT")}
6670

@@ -252,3 +256,35 @@ func (p *powerVSCloud) GetDiskByID(volumeID string) (disk *Disk, err error) {
252256
CapacityGiB: int64(*v.Size),
253257
}, nil
254258
}
259+
260+
func readCredentials() (string, error) {
261+
apiKey, err := readCredentialsFromFile()
262+
if err != nil {
263+
return "", err
264+
}
265+
if apiKey != "" {
266+
return apiKey, nil
267+
}
268+
269+
klog.Info("Falling back to read IBMCLOUD_API_KEY environment variable for the key")
270+
apiKey = os.Getenv("IBMCLOUD_API_KEY")
271+
if apiKey == "" {
272+
return "", fmt.Errorf("IBMCLOUD_API_KEY is not provided")
273+
}
274+
275+
return apiKey, nil
276+
}
277+
278+
func readCredentialsFromFile() (string, error) {
279+
apiKeyPath := os.Getenv("API_KEY_PATH")
280+
if apiKeyPath == "" {
281+
klog.Warning("API_KEY_PATH is undefined")
282+
return "", nil
283+
}
284+
285+
byteData, err := os.ReadFile(apiKeyPath)
286+
if err != nil {
287+
return "", fmt.Errorf("error reading apikey: %v", err)
288+
}
289+
return string(byteData), nil
290+
}

0 commit comments

Comments
 (0)