blob: 1537167a0df6acc69a1726e7895f0e4b61e68f14 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1beta2
18
19import (
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.
29type DeploymentListerExpansion interface {
30 GetDeploymentsForReplicaSet(rs *apps.ReplicaSet) ([]*apps.Deployment, error)
31}
32
33// DeploymentNamespaceListerExpansion allows custom methods to be added to
34// DeploymentNamespaceLister.
35type 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.
41func (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}