Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package v1beta2 |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | |
| 22 | apps "k8s.io/api/apps/v1beta2" |
| 23 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | "k8s.io/apimachinery/pkg/labels" |
| 25 | ) |
| 26 | |
| 27 | // DeploymentListerExpansion allows custom methods to be added to |
| 28 | // DeploymentLister. |
| 29 | type DeploymentListerExpansion interface { |
| 30 | GetDeploymentsForReplicaSet(rs *apps.ReplicaSet) ([]*apps.Deployment, error) |
| 31 | } |
| 32 | |
| 33 | // DeploymentNamespaceListerExpansion allows custom methods to be added to |
| 34 | // DeploymentNamespaceLister. |
| 35 | type DeploymentNamespaceListerExpansion interface{} |
| 36 | |
| 37 | // GetDeploymentsForReplicaSet returns a list of Deployments that potentially |
| 38 | // match a ReplicaSet. Only the one specified in the ReplicaSet's ControllerRef |
| 39 | // will actually manage it. |
| 40 | // Returns an error only if no matching Deployments are found. |
| 41 | func (s *deploymentLister) GetDeploymentsForReplicaSet(rs *apps.ReplicaSet) ([]*apps.Deployment, error) { |
| 42 | if len(rs.Labels) == 0 { |
| 43 | return nil, fmt.Errorf("no deployments found for ReplicaSet %v because it has no labels", rs.Name) |
| 44 | } |
| 45 | |
| 46 | // TODO: MODIFY THIS METHOD so that it checks for the podTemplateSpecHash label |
| 47 | dList, err := s.Deployments(rs.Namespace).List(labels.Everything()) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | var deployments []*apps.Deployment |
| 53 | for _, d := range dList { |
| 54 | selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector) |
| 55 | if err != nil { |
| 56 | return nil, fmt.Errorf("invalid label selector: %v", err) |
| 57 | } |
| 58 | // If a deployment with a nil or empty selector creeps in, it should match nothing, not everything. |
| 59 | if selector.Empty() || !selector.Matches(labels.Set(rs.Labels)) { |
| 60 | continue |
| 61 | } |
| 62 | deployments = append(deployments, d) |
| 63 | } |
| 64 | |
| 65 | if len(deployments) == 0 { |
| 66 | return nil, fmt.Errorf("could not find deployments set for ReplicaSet %s in namespace %s with labels: %v", rs.Name, rs.Namespace, rs.Labels) |
| 67 | } |
| 68 | |
| 69 | return deployments, nil |
| 70 | } |