2019-03-07 21:03:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 ceph-csi authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
k8s "k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
K8sConfig is a ConfigStore interface implementation that reads configuration
|
|
|
|
information from k8s secrets.
|
|
|
|
|
|
|
|
Each Ceph cluster configuration secret is expected to be named,
|
2019-03-12 15:57:36 +00:00
|
|
|
ceph-cluster-<clusterid>, where <clusterid> uniquely identifies and
|
|
|
|
separates the each Ceph cluster configuration.
|
2019-03-07 21:03:33 +00:00
|
|
|
|
|
|
|
The secret is expected to contain keys, as defined by the ConfigKeys constants
|
|
|
|
in the ConfigStore interface.
|
|
|
|
*/
|
|
|
|
type K8sConfig struct {
|
|
|
|
Client *k8s.Clientset
|
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:57:36 +00:00
|
|
|
// DataForKey reads the appropriate k8s secret, named using clusterid, and
|
|
|
|
// returns the contents of key within the secret
|
2019-03-13 13:46:56 +00:00
|
|
|
func (kc *K8sConfig) DataForKey(clusterid, key string) (data string, err error) {
|
2019-03-12 15:57:36 +00:00
|
|
|
secret, err := kc.Client.CoreV1().Secrets(kc.Namespace).Get("ceph-cluster-"+clusterid, metav1.GetOptions{})
|
2019-03-07 21:03:33 +00:00
|
|
|
if err != nil {
|
2019-03-12 15:57:36 +00:00
|
|
|
err = fmt.Errorf("error fetching configuration for cluster ID (%s). (%s)", clusterid, err)
|
2019-03-07 21:03:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, ok := secret.Data[key]
|
|
|
|
if !ok {
|
2019-03-12 15:57:36 +00:00
|
|
|
err = fmt.Errorf("missing data for key (%s) in cluster configuration of (%s)", key, clusterid)
|
2019-03-07 21:03:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data = string(content)
|
|
|
|
return
|
|
|
|
}
|