|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package operator |
| 24 | + |
| 25 | +import ( |
| 26 | + "sort" |
| 27 | + |
| 28 | + "github.com/rs/zerolog" |
| 29 | + "k8s.io/api/core/v1" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/server" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + appKey = "app" |
| 38 | + roleKey = "role" |
| 39 | + appDeploymentOperator = "arango-deployment-operator" |
| 40 | + appDeploymentReplicationOperator = "arango-deployment-replication-operator" |
| 41 | + appStorageOperator = "arango-storage-operator" |
| 42 | + roleLeader = "leader" |
| 43 | +) |
| 44 | + |
| 45 | +// FindOtherOperators looks up references to other operators in the same Kubernetes cluster. |
| 46 | +func (o *Operator) FindOtherOperators() []server.OperatorReference { |
| 47 | + log := o.log |
| 48 | + var result []server.OperatorReference |
| 49 | + namespaces, err := o.Dependencies.KubeCli.CoreV1().Namespaces().List(metav1.ListOptions{}) |
| 50 | + if err != nil { |
| 51 | + log.Warn().Err(err).Msg("Failed to list namespaces") |
| 52 | + } else { |
| 53 | + for _, ns := range namespaces.Items { |
| 54 | + if ns.Name != o.Config.Namespace { |
| 55 | + log.Debug().Str("namespace", ns.Name).Msg("inspecting namespace for operators") |
| 56 | + refs := o.findOtherOperatorsInNamespace(log, ns.Name, func(server.OperatorType) bool { return true }) |
| 57 | + result = append(result, refs...) |
| 58 | + } else { |
| 59 | + log.Debug().Str("namespace", ns.Name).Msg("skip inspecting my own namespace for operators") |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + refs := o.findOtherOperatorsInNamespace(log, o.Config.Namespace, func(oType server.OperatorType) bool { |
| 64 | + // Exclude those operators that I provide myself. |
| 65 | + switch oType { |
| 66 | + case server.OperatorTypeDeployment: |
| 67 | + return !o.Dependencies.DeploymentProbe.IsReady() |
| 68 | + case server.OperatorTypeDeploymentReplication: |
| 69 | + return !o.Dependencies.DeploymentReplicationProbe.IsReady() |
| 70 | + case server.OperatorTypeStorage: |
| 71 | + return !o.Dependencies.StorageProbe.IsReady() |
| 72 | + default: |
| 73 | + return true |
| 74 | + } |
| 75 | + }) |
| 76 | + result = append(result, refs...) |
| 77 | + sort.Slice(result, func(i, j int) bool { |
| 78 | + if result[i].Namespace == result[j].Namespace { |
| 79 | + return result[i].Type < result[j].Type |
| 80 | + } |
| 81 | + return result[i].Namespace < result[j].Namespace |
| 82 | + }) |
| 83 | + |
| 84 | + return result |
| 85 | +} |
| 86 | + |
| 87 | +// findOtherOperatorsInNamespace looks up references to other operators in the given namespace. |
| 88 | +func (o *Operator) findOtherOperatorsInNamespace(log zerolog.Logger, namespace string, typePred func(server.OperatorType) bool) []server.OperatorReference { |
| 89 | + log = log.With().Str("namespace", namespace).Logger() |
| 90 | + var result []server.OperatorReference |
| 91 | + services, err := o.Dependencies.KubeCli.CoreV1().Services(namespace).List(metav1.ListOptions{}) |
| 92 | + if err != nil { |
| 93 | + log.Debug().Err(err).Msg("Failed to list services") |
| 94 | + return nil |
| 95 | + } |
| 96 | + nodeFetcher := func() (v1.NodeList, error) { |
| 97 | + result, err := o.Dependencies.KubeCli.CoreV1().Nodes().List(metav1.ListOptions{}) |
| 98 | + if err != nil { |
| 99 | + return v1.NodeList{}, maskAny(err) |
| 100 | + } |
| 101 | + return *result, nil |
| 102 | + } |
| 103 | + for _, svc := range services.Items { |
| 104 | + // Filter out unwanted services |
| 105 | + selector := svc.Spec.Selector |
| 106 | + if selector[roleKey] != roleLeader { |
| 107 | + log.Debug().Str("service", svc.Name).Msg("Service has no leader role selector") |
| 108 | + continue |
| 109 | + } |
| 110 | + var oType server.OperatorType |
| 111 | + switch selector[appKey] { |
| 112 | + case appDeploymentOperator: |
| 113 | + oType = server.OperatorTypeDeployment |
| 114 | + case appDeploymentReplicationOperator: |
| 115 | + oType = server.OperatorTypeDeploymentReplication |
| 116 | + case appStorageOperator: |
| 117 | + oType = server.OperatorTypeStorage |
| 118 | + default: |
| 119 | + log.Debug().Str("service", svc.Name).Msg("Service has no or invalid app selector") |
| 120 | + continue |
| 121 | + } |
| 122 | + if !typePred(oType) { |
| 123 | + continue |
| 124 | + } |
| 125 | + var url string |
| 126 | + switch svc.Spec.Type { |
| 127 | + case v1.ServiceTypeNodePort, v1.ServiceTypeLoadBalancer: |
| 128 | + if x, err := k8sutil.CreateServiceURL(svc, "https", nil, nodeFetcher); err == nil { |
| 129 | + url = x |
| 130 | + } else { |
| 131 | + log.Warn().Err(err).Str("service", svc.Name).Msg("Failed to create URL for service") |
| 132 | + } |
| 133 | + default: |
| 134 | + // No suitable service type |
| 135 | + continue |
| 136 | + } |
| 137 | + result = append(result, server.OperatorReference{ |
| 138 | + Namespace: svc.GetNamespace(), |
| 139 | + URL: url, |
| 140 | + Type: oType, |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + log.Debug().Msgf("Found %d operator services", len(result)) |
| 145 | + return result |
| 146 | +} |
0 commit comments