Files
my-rook-config/pkg/operator/k8sutil/pod.go
T
Steve Leon 236468119e Added volume provisioner to Rook
- The provisioner is a Kubernetes external volume provisioner
- It calls the Rook API to create and delete volumes on behalf
  of Kubernetes
- It automatically discover Ceph monitor endpoints
- It locates RBD ceph user keys based on the Rook cluster location
- It uses the in-tree RBD volume plugin to do the map/unmap
- Also modify the documentation to reflect the changes on the
  storageclass
2017-04-17 15:20:44 -07:00

109 lines
2.7 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
import (
"encoding/json"
"fmt"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
)
const (
AppAttr = "app"
ClusterAttr = "rook_cluster"
VersionAttr = "rook_version"
PodIPEnvVar = "ROOKD_PRIVATE_IPV4"
DefaultRepoPrefix = "quay.io/rook"
repoPrefixEnvVar = "ROOKD_REPO_PREFIX"
defaultVersion = "latest"
)
func NamespaceEnvVar() v1.EnvVar {
return v1.EnvVar{Name: "ROOKD_NAMESPACE", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}}}
}
func RepoPrefixEnvVar() v1.EnvVar {
return v1.EnvVar{Name: repoPrefixEnvVar, Value: repoPrefix()}
}
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
}
func MakeRookImage(version string) string {
return fmt.Sprintf("%s/rookd:%v", repoPrefix(), getVersion(version))
}
func PodWithAntiAffinity(pod *v1.Pod, attribute, value string) {
// set pod anti-affinity with the pods that belongs to the same rook cluster
affinity := v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
attribute: value,
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
}
affinityb, err := json.Marshal(affinity)
if err != nil {
panic("failed to marshal affinty struct")
}
pod.Annotations[v1.AffinityAnnotationKey] = string(affinityb)
}
func SetPodVersion(pod *v1.Pod, key, version string) {
pod.Annotations[key] = version
}
func GetPodNames(pods []*api.Pod) []string {
res := []string{}
for _, p := range pods {
res = append(res, p.Name)
}
return res
}