-
Notifications
You must be signed in to change notification settings - Fork 38.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a DeploymentDescriber to kubectl #14199
Merged
nikhiljindal
merged 1 commit into
kubernetes:master
from
nikhiljindal:deploymentDescribe
Oct 1, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2015 The Kubernetes 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. | ||
*/ | ||
|
||
package deployment | ||
|
||
import ( | ||
"fmt" | ||
"hash/adler32" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/apis/experimental" | ||
client "k8s.io/kubernetes/pkg/client/unversioned" | ||
"k8s.io/kubernetes/pkg/fields" | ||
"k8s.io/kubernetes/pkg/labels" | ||
"k8s.io/kubernetes/pkg/util" | ||
) | ||
|
||
// Returns the old RCs targetted by the given Deployment. | ||
func GetOldRCs(deployment experimental.Deployment, c client.Interface) ([]*api.ReplicationController, error) { | ||
namespace := deployment.ObjectMeta.Namespace | ||
// 1. Find all pods whose labels match deployment.Spec.Selector | ||
podList, err := c.Pods(namespace).List(labels.SelectorFromSet(deployment.Spec.Selector), fields.Everything()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing pods: %v", err) | ||
} | ||
// 2. Find the corresponding RCs for pods in podList. | ||
// TODO: Right now we list all RCs and then filter. We should add an API for this. | ||
oldRCs := map[string]api.ReplicationController{} | ||
rcList, err := c.ReplicationControllers(namespace).List(labels.Everything()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing replication controllers: %v", err) | ||
} | ||
for _, pod := range podList.Items { | ||
podLabelsSelector := labels.Set(pod.ObjectMeta.Labels) | ||
for _, rc := range rcList.Items { | ||
rcLabelsSelector := labels.SelectorFromSet(rc.Spec.Selector) | ||
if rcLabelsSelector.Matches(podLabelsSelector) { | ||
// Filter out RC that has the same pod template spec as the deployment - that is the new RC. | ||
if api.Semantic.DeepEqual(rc.Spec.Template, GetNewRCTemplate(deployment)) { | ||
continue | ||
} | ||
oldRCs[rc.ObjectMeta.Name] = rc | ||
} | ||
} | ||
} | ||
requiredRCs := []*api.ReplicationController{} | ||
for _, value := range oldRCs { | ||
requiredRCs = append(requiredRCs, &value) | ||
} | ||
return requiredRCs, nil | ||
} | ||
|
||
// Returns an RC that matches the intent of the given deployment. | ||
// Returns nil if the new RC doesnt exist yet. | ||
func GetNewRC(deployment experimental.Deployment, c client.Interface) (*api.ReplicationController, error) { | ||
namespace := deployment.ObjectMeta.Namespace | ||
rcList, err := c.ReplicationControllers(namespace).List(labels.Everything()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing replication controllers: %v", err) | ||
} | ||
newRCTemplate := GetNewRCTemplate(deployment) | ||
|
||
for _, rc := range rcList.Items { | ||
if api.Semantic.DeepEqual(rc.Spec.Template, newRCTemplate) { | ||
// This is the new RC. | ||
return &rc, nil | ||
} | ||
} | ||
// new RC does not exist. | ||
return nil, nil | ||
} | ||
|
||
// Returns the desired PodTemplateSpec for the new RC corresponding to the given RC. | ||
func GetNewRCTemplate(deployment experimental.Deployment) *api.PodTemplateSpec { | ||
// newRC will have the same template as in deployment spec, plus a unique label in some cases. | ||
newRCTemplate := &api.PodTemplateSpec{ | ||
ObjectMeta: deployment.Spec.Template.ObjectMeta, | ||
Spec: deployment.Spec.Template.Spec, | ||
} | ||
podTemplateSpecHash := GetPodTemplateSpecHash(newRCTemplate) | ||
if deployment.Spec.UniqueLabelKey != "" { | ||
newLabels := map[string]string{} | ||
for key, value := range deployment.Spec.Template.ObjectMeta.Labels { | ||
newLabels[key] = value | ||
} | ||
newLabels[deployment.Spec.UniqueLabelKey] = fmt.Sprintf("%d", podTemplateSpecHash) | ||
newRCTemplate.ObjectMeta.Labels = newLabels | ||
} | ||
return newRCTemplate | ||
} | ||
|
||
func GetPodTemplateSpecHash(template *api.PodTemplateSpec) uint32 { | ||
podTemplateSpecHasher := adler32.New() | ||
util.DeepHashObject(podTemplateSpecHasher, template) | ||
return podTemplateSpecHasher.Sum32() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should get the replication controllers managed by this dep and show them in some order. If they can be loaded we shouldn't hard fail (show error and continue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we really want them, then instead of recomputing them here, I would prefer that deployment controller adds them to the DeploymentStatus and we just display it here.
I think we had discussed doing that in the proposal, but then decided against it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree with @smarterclayton here. Keeping copies of other objects' state in the Deployment object's status will either be frequently out of date or will lead to lots of extraneous updates to apiserver. At the very most, perhaps the matching RC names should be in the status (and, during a deployment, both the old RC's and the new RC's).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Thats the reason why we decided not to store anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comparison I'm drawing is to the pod describer where we fetch the replication controllers that overlap the pod. I think users would expect the same here - I'm ok with showing status, but it is common to want to know whether you have multiple RCs in flight during a deployment. Getting the list of RCs and even showing what the D controller thinks is the status is both good. The note about not failing was just that if the RC is borked for some reason we don't want to prevent describe from working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done