37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
workload_common.go 23.63 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
package workload
import (
"strconv"
"strings"
"fmt"
"encoding/json"
"github.com/rancher/types/apis/apps/v1beta2"
batchv1 "github.com/rancher/types/apis/batch/v1"
"github.com/rancher/types/apis/batch/v1beta1"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
corev1beta2 "k8s.io/api/apps/v1beta2"
corebatchv1 "k8s.io/api/batch/v1"
corebatchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
AppVersion = "apps/v1beta2"
BatchBetaVersion = "batch/v1beta1"
BatchVersion = "batch/v1"
WorkloadAnnotation = "field.cattle.io/targetWorkloadIds"
PortsAnnotation = "field.cattle.io/ports"
ClusterIPServiceType = "ClusterIP"
DeploymentType = "deployment"
ReplicationControllerType = "replicationcontroller"
ReplicaSetType = "replicaset"
DaemonSetType = "daemonset"
StatefulSetType = "statefulset"
JobType = "job"
CronJobType = "cronjob"
WorkloadAnnotatioNoop = "workload.cattle.io/targetWorkloadIdNoop"
WorkloaAnnotationdPortBasedService = "workload.cattle.io/workloadPortBased"
)
var WorkloadKinds = map[string]bool{
"Deployment": true,
"ReplicationController": true,
"ReplicaSet": true,
"DaemonSet": true,
"StatefulSet": true,
"Job": true,
"CronJob": true,
}
type Workload struct {
Name string
Namespace string
UUID types.UID
SelectorLabels map[string]string
Annotations map[string]string
TemplateSpec *corev1.PodTemplateSpec
Kind string
APIVersion string
OwnerReferences []metav1.OwnerReference
Labels map[string]string
Key string
Status *Status
}
type Status struct {
Replicas int32
AvailableReplicas int32
}
type CommonController struct {
DeploymentLister v1beta2.DeploymentLister
ReplicationControllerLister v1.ReplicationControllerLister
ReplicaSetLister v1beta2.ReplicaSetLister
DaemonSetLister v1beta2.DaemonSetLister
StatefulSetLister v1beta2.StatefulSetLister
JobLister batchv1.JobLister
CronJobLister v1beta1.CronJobLister
Deployments v1beta2.DeploymentInterface
ReplicationControllers v1.ReplicationControllerInterface
ReplicaSets v1beta2.ReplicaSetInterface
DaemonSets v1beta2.DaemonSetInterface
StatefulSets v1beta2.StatefulSetInterface
Jobs batchv1.JobInterface
CronJobs v1beta1.CronJobInterface
Sync func(key string, w *Workload) error
}
func NewWorkloadController(workload *config.UserOnlyContext, f func(key string, w *Workload) error) CommonController {
c := CommonController{
DeploymentLister: workload.Apps.Deployments("").Controller().Lister(),
ReplicationControllerLister: workload.Core.ReplicationControllers("").Controller().Lister(),
ReplicaSetLister: workload.Apps.ReplicaSets("").Controller().Lister(),
DaemonSetLister: workload.Apps.DaemonSets("").Controller().Lister(),
StatefulSetLister: workload.Apps.StatefulSets("").Controller().Lister(),
JobLister: workload.BatchV1.Jobs("").Controller().Lister(),
CronJobLister: workload.BatchV1Beta1.CronJobs("").Controller().Lister(),
Deployments: workload.Apps.Deployments(""),
ReplicationControllers: workload.Core.ReplicationControllers(""),
ReplicaSets: workload.Apps.ReplicaSets(""),
DaemonSets: workload.Apps.DaemonSets(""),
StatefulSets: workload.Apps.StatefulSets(""),
Jobs: workload.BatchV1.Jobs(""),
CronJobs: workload.BatchV1Beta1.CronJobs(""),
Sync: f,
}
if f != nil {
workload.Apps.Deployments("").AddHandler(getName(), c.syncDeployments)
workload.Core.ReplicationControllers("").AddHandler(getName(), c.syncReplicationControllers)
workload.Apps.ReplicaSets("").AddHandler(getName(), c.syncReplicaSet)
workload.Apps.DaemonSets("").AddHandler(getName(), c.syncDaemonSet)
workload.Apps.StatefulSets("").AddHandler(getName(), c.syncStatefulSet)
workload.BatchV1.Jobs("").AddHandler(getName(), c.syncJob)
workload.BatchV1Beta1.CronJobs("").AddHandler(getName(), c.syncCronJob)
}
return c
}
func (c *CommonController) syncDeployments(key string, obj *corev1beta2.Deployment) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, DeploymentType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncReplicationControllers(key string, obj *corev1.ReplicationController) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, ReplicationControllerType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncReplicaSet(key string, obj *corev1beta2.ReplicaSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, ReplicaSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncDaemonSet(key string, obj *corev1beta2.DaemonSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, DaemonSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncStatefulSet(key string, obj *corev1beta2.StatefulSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, StatefulSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncJob(key string, obj *corebatchv1.Job) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, JobType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncCronJob(key string, obj *corebatchv1beta1.CronJob) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, CronJobType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c CommonController) getWorkload(key string, objectType string) (*Workload, error) {
splitted := strings.Split(key, "/")
namespace := splitted[0]
name := splitted[1]
return c.GetByWorkloadID(getWorkloadID(objectType, namespace, name))
}
func (c CommonController) GetByWorkloadID(key string) (*Workload, error) {
return c.getByWorkloadIDFromCacheOrAPI(key, false)
}
func (c CommonController) GetByWorkloadIDRetryAPIIfNotFound(key string) (*Workload, error) {
return c.getByWorkloadIDFromCacheOrAPI(key, true)
}
func (c CommonController) getByWorkloadIDFromCacheOrAPI(key string, retry bool) (*Workload, error) {
splitted := strings.Split(key, ":")
if len(splitted) != 3 {
return nil, fmt.Errorf("workload name [%s] is invalid", key)
}
workloadType := strings.ToLower(splitted[0])
namespace := splitted[1]
name := splitted[2]
var workload *Workload
switch workloadType {
case ReplicationControllerType:
o, err := c.ReplicationControllerLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.ReplicationControllers.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
labelSelector := &metav1.LabelSelector{
MatchLabels: o.Spec.Selector,
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, labelSelector, o.Annotations, o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
case ReplicaSetType:
o, err := c.ReplicaSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.ReplicaSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
case DaemonSetType:
o, err := c.DaemonSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.DaemonSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.DesiredNumberScheduled, o.Status.NumberAvailable)
case StatefulSetType:
o, err := c.StatefulSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.StatefulSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.ReadyReplicas)
case JobType:
o, err := c.JobLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.Jobs.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
var labelSelector *metav1.LabelSelector
if o.Spec.Selector != nil {
labelSelector = &metav1.LabelSelector{
MatchLabels: o.Spec.Selector.MatchLabels,
}
}
workload = getWorkload(namespace, name, workloadType, BatchVersion, o.UID, labelSelector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, 0, 0)
case CronJobType:
o, err := c.CronJobLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.CronJobs.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
var labelSelector *metav1.LabelSelector
if o.Spec.JobTemplate.Spec.Selector != nil {
labelSelector = &metav1.LabelSelector{
MatchLabels: o.Spec.JobTemplate.Spec.Selector.MatchLabels,
}
}
workload = getWorkload(namespace, name, workloadType, BatchBetaVersion, o.UID, labelSelector, o.Annotations, &o.Spec.JobTemplate.Spec.Template, o.OwnerReferences, o.Labels, 0, 0)
default:
o, err := c.DeploymentLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.Deployments.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, DeploymentType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
}
return workload, nil
}
func getWorkload(namespace string, name string, kind string, apiVersion string, UUID types.UID, selectorLabels *metav1.LabelSelector,
annotations map[string]string, podTemplateSpec *corev1.PodTemplateSpec, ownerRefs []metav1.OwnerReference, labels map[string]string,
replicas, availableReplicas int32) *Workload {
w := &Workload{
Name: name,
Namespace: namespace,
SelectorLabels: getSelectorLables(selectorLabels),
UUID: UUID,
Annotations: annotations,
TemplateSpec: podTemplateSpec,
OwnerReferences: ownerRefs,
Kind: kind,
APIVersion: apiVersion,
Labels: labels,
Key: fmt.Sprintf("%s:%s:%s", kind, namespace, name),
Status: &Status{
Replicas: replicas,
AvailableReplicas: availableReplicas,
},
}
return w
}
func (c CommonController) GetAllWorkloads(namespace string) ([]*Workload, error) {
var workloads []*Workload
// deployments
ds, err := c.DeploymentLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range ds {
workload, err := c.GetByWorkloadID(getWorkloadID(DeploymentType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// replication controllers
rcs, err := c.ReplicationControllerLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range rcs {
workload, err := c.GetByWorkloadID(getWorkloadID(ReplicationControllerType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// replica sets
rss, err := c.ReplicaSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range rss {
workload, err := c.GetByWorkloadID(getWorkloadID(ReplicaSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// daemon sets
dss, err := c.DaemonSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range dss {
workload, err := c.GetByWorkloadID(getWorkloadID(DaemonSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// stateful sets
sts, err := c.StatefulSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range sts {
workload, err := c.GetByWorkloadID(getWorkloadID(StatefulSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// jobs
jobs, err := c.JobLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range jobs {
workload, err := c.GetByWorkloadID(getWorkloadID(JobType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// cron jobs
cronJobs, err := c.CronJobLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range cronJobs {
workload, err := c.GetByWorkloadID(getWorkloadID(CronJobType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
return workloads, nil
}
func (c CommonController) GetWorkloadsMatchingLabels(namespace string, targetLabels map[string]string) ([]*Workload, error) {
var workloads []*Workload
allWorkloads, err := c.GetAllWorkloads(namespace)
if err != nil {
return workloads, err
}
for _, workload := range allWorkloads {
workloadSelector := labels.SelectorFromSet(workload.SelectorLabels)
if workloadSelector.Matches(labels.Set(targetLabels)) {
workloads = append(workloads, workload)
}
}
return workloads, nil
}
func (c CommonController) GetWorkloadsMatchingSelector(namespace string, selectorLabels map[string]string) ([]*Workload, error) {
var workloads []*Workload
allWorkloads, err := c.GetAllWorkloads(namespace)
if err != nil {
return workloads, err
}
selector := labels.SelectorFromSet(selectorLabels)
for _, workload := range allWorkloads {
if selector.Matches(labels.Set(workload.Labels)) {
workloads = append(workloads, workload)
}
}
return workloads, nil
}
func getSelectorLables(s *metav1.LabelSelector) map[string]string {
if s == nil {
return nil
}
selectorLabels := map[string]string{}
for key, value := range s.MatchLabels {
selectorLabels[key] = value
}
return selectorLabels
}
type Service struct {
Type corev1.ServiceType
ClusterIP string
ServicePorts []corev1.ServicePort
Name string
}
type ContainerPort struct {
Kind string `json:"kind,omitempty"`
SourcePort int `json:"sourcePort,omitempty"`
DNSName string `json:"dnsName,omitempty"`
Name string `json:"name,omitempty"`
Protocol string `json:"protocol,omitempty"`
ContainerPort int32 `json:"containerPort,omitempty"`
}
func generateClusterIPServiceFromContainers(workload *Workload) *Service {
var servicePorts []corev1.ServicePort
for _, c := range workload.TemplateSpec.Spec.Containers {
for _, p := range c.Ports {
var portName string
if p.Name == "" {
portName = fmt.Sprintf("%s-%s", strconv.FormatInt(int64(p.ContainerPort), 10), c.Name)
} else {
portName = fmt.Sprintf("%s-%s", p.Name, c.Name)
}
servicePort := corev1.ServicePort{
Port: p.ContainerPort,
TargetPort: intstr.Parse(strconv.FormatInt(int64(p.ContainerPort), 10)),
Protocol: p.Protocol,
Name: portName,
}
servicePorts = append(servicePorts, servicePort)
}
}
clusterIP := ""
// append default port as sky dns won't work w/o at least one port being set
if len(servicePorts) == 0 {
servicePort := corev1.ServicePort{
Port: 42,
TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
Protocol: corev1.Protocol(corev1.ProtocolTCP),
Name: "default",
}
clusterIP = "None"
servicePorts = append(servicePorts, servicePort)
}
return &Service{
Type: ClusterIPServiceType,
ClusterIP: clusterIP,
ServicePorts: servicePorts,
Name: workload.Name,
}
}
func generateServicesFromPortsAnnotation(workload *Workload) ([]Service, error) {
var services []Service
val, ok := workload.TemplateSpec.Annotations[PortsAnnotation]
if !ok {
return services, nil
}
var portList [][]ContainerPort
err := json.Unmarshal([]byte(val), &portList)
if err != nil {
return services, err
}
svcTypeToPort := map[corev1.ServiceType][]ContainerPort{}
for _, l := range portList {
for _, port := range l {
if port.Kind == "HostPort" {
continue
}
svcType := corev1.ServiceType(port.Kind)
svcTypeToPort[svcType] = append(svcTypeToPort[svcType], port)
}
}
for svcType, ports := range svcTypeToPort {
servicePorts := map[string][]corev1.ServicePort{}
for _, p := range ports {
var nodePort int32
var clusterIPPort int32
if svcType == corev1.ServiceTypeNodePort {
nodePort = int32(p.SourcePort)
clusterIPPort = p.ContainerPort
} else {
if p.SourcePort == 0 {
clusterIPPort = p.ContainerPort
} else {
clusterIPPort = int32(p.SourcePort)
}
}
servicePort := corev1.ServicePort{
Port: clusterIPPort,
TargetPort: intstr.Parse(strconv.FormatInt(int64(p.ContainerPort), 10)),
NodePort: nodePort,
Protocol: corev1.Protocol(p.Protocol),
Name: p.Name,
}
dnsName := p.DNSName
if dnsName == "" {
dnsName = workload.Name
}
servicePorts[dnsName] = append(servicePorts[dnsName], servicePort)
}
// append default port as sky dns won't work w/o at least one port being set
if len(servicePorts) == 0 {
servicePort := corev1.ServicePort{
Port: 42,
TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
Protocol: corev1.Protocol(corev1.ProtocolTCP),
Name: "default",
}
servicePorts[workload.Name] = append(servicePorts[workload.Name], servicePort)
}
for dnsName, servicePorts := range servicePorts {
services = append(services, Service{
Type: svcType,
ServicePorts: servicePorts,
Name: dnsName,
})
}
}
return services, nil
}
func getWorkloadID(objectType string, namespace string, name string) string {
return fmt.Sprintf("%s:%s:%s", objectType, namespace, name)
}
func (c CommonController) UpdateWorkload(w *Workload, annotations map[string]string) error {
// only annotations updates are supported
switch w.Kind {
case DeploymentType:
o, err := c.DeploymentLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.Deployments.Update(toUpdate)
if err != nil {
return err
}
case ReplicationControllerType:
o, err := c.ReplicationControllerLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.ReplicationControllers.Update(toUpdate)
if err != nil {
return err
}
case ReplicaSetType:
o, err := c.ReplicaSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.ReplicaSets.Update(toUpdate)
if err != nil {
return err
}
case DaemonSetType:
o, err := c.DaemonSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.DaemonSets.Update(toUpdate)
if err != nil {
return err
}
case StatefulSetType:
o, err := c.StatefulSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.StatefulSets.Update(toUpdate)
if err != nil {
return err
}
case JobType:
o, err := c.JobLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.Jobs.Update(toUpdate)
if err != nil {
return err
}
case CronJobType:
o, err := c.CronJobLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.CronJobs.Update(toUpdate)
if err != nil {
return err
}
}
return nil
}
func (c CommonController) EnqueueWorkload(w *Workload) {
switch w.Kind {
case DeploymentType:
c.Deployments.Controller().Enqueue(w.Namespace, w.Name)
case ReplicationControllerType:
c.ReplicationControllers.Controller().Enqueue(w.Namespace, w.Name)
case ReplicaSetType:
c.ReplicaSets.Controller().Enqueue(w.Namespace, w.Name)
case DaemonSetType:
c.DaemonSets.Controller().Enqueue(w.Namespace, w.Name)
case StatefulSetType:
c.StatefulSets.Controller().Enqueue(w.Namespace, w.Name)
case JobType:
c.Jobs.Controller().Enqueue(w.Namespace, w.Name)
case CronJobType:
c.CronJobs.Controller().Enqueue(w.Namespace, w.Name)
}
}
func (c CommonController) EnqueueAllWorkloads(namespace string) error {
ws, err := c.GetAllWorkloads(namespace)
if err != nil {
return err
}
for _, w := range ws {
c.EnqueueWorkload(w)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.7

搜索帮助

0d507c66 1850385 C8b1a773 1850385