109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
/*
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
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.
|
|
|
|
Some of the code below came from https://github.com/coreos/etcd-operator
|
|
which also has the apache 2.0 license.
|
|
*/
|
|
|
|
// Package k8sutil for Kubernetes helpers.
|
|
package k8sutil
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
|
|
"k8s.io/api/core/v1"
|
|
)
|
|
|
|
const (
|
|
// AppAttr app label
|
|
AppAttr = "app"
|
|
// ClusterAttr cluster label
|
|
ClusterAttr = "rook_cluster"
|
|
// VersionAttr version label
|
|
VersionAttr = "rook_version"
|
|
// PodIPEnvVar pod IP env var
|
|
PodIPEnvVar = "ROOKD_PRIVATE_IPV4"
|
|
// DefaultRepoPrefix repo prefix
|
|
DefaultRepoPrefix = "rook"
|
|
// ConfigOverrideName config override name
|
|
ConfigOverrideName = "rook-config-override"
|
|
// ConfigOverrideVal config override value
|
|
ConfigOverrideVal = "config"
|
|
repoPrefixEnvVar = "ROOKD_REPO_PREFIX"
|
|
defaultVersion = "latest"
|
|
configMountDir = "/etc/rook"
|
|
overrideFilename = "override.conf"
|
|
)
|
|
|
|
// ConfigOverrideMount is an override mount
|
|
func ConfigOverrideMount() v1.VolumeMount {
|
|
return v1.VolumeMount{Name: ConfigOverrideName, MountPath: configMountDir}
|
|
}
|
|
|
|
// ConfigOverrideVolume is an override volume
|
|
func ConfigOverrideVolume() v1.Volume {
|
|
cmSource := &v1.ConfigMapVolumeSource{Items: []v1.KeyToPath{{Key: ConfigOverrideVal, Path: overrideFilename}}}
|
|
cmSource.Name = ConfigOverrideName
|
|
return v1.Volume{Name: ConfigOverrideName, VolumeSource: v1.VolumeSource{ConfigMap: cmSource}}
|
|
}
|
|
|
|
// ConfigOverrideEnvVar config override env var
|
|
func ConfigOverrideEnvVar() v1.EnvVar {
|
|
return v1.EnvVar{Name: "ROOKD_CEPH_CONFIG_OVERRIDE", Value: path.Join(configMountDir, overrideFilename)}
|
|
}
|
|
|
|
// NamespaceEnvVar namespace env var
|
|
func NamespaceEnvVar() v1.EnvVar {
|
|
return v1.EnvVar{Name: "ROOKD_NAMESPACE", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}}}
|
|
}
|
|
|
|
// RepoPrefixEnvVar repo prefix env var
|
|
func RepoPrefixEnvVar() v1.EnvVar {
|
|
return v1.EnvVar{Name: repoPrefixEnvVar, Value: repoPrefix()}
|
|
}
|
|
|
|
// ConfigDirEnvVar config dir env var
|
|
func ConfigDirEnvVar() v1.EnvVar {
|
|
return v1.EnvVar{Name: "ROOKD_CONFIG_DIR", Value: DataDir}
|
|
}
|
|
|
|
func repoPrefix() string {
|
|
r := os.Getenv(repoPrefixEnvVar)
|
|
if r == "" {
|
|
r = DefaultRepoPrefix
|
|
}
|
|
return r
|
|
}
|
|
|
|
func getVersion(version string) string {
|
|
if version == "" {
|
|
version = defaultVersion
|
|
}
|
|
|
|
return version
|
|
}
|
|
|
|
// MakeRookImage formats the container name
|
|
func MakeRookImage(version string) string {
|
|
return fmt.Sprintf("%s/rook:%v", repoPrefix(), getVersion(version))
|
|
}
|
|
|
|
// SetPodVersion sets the pod annotation
|
|
func SetPodVersion(pod *v1.Pod, key, version string) {
|
|
pod.Annotations[key] = version
|
|
}
|