Make deployments work.

This commit is contained in:
Madhusudan.C.S
2016-02-05 18:43:02 -08:00
parent 518f08aa7c
commit ed7ad6dcf3
28 changed files with 485 additions and 385 deletions

View File

@@ -291,6 +291,7 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
}
rf := cmdutil.NewFactory(nil)
f.PodSelectorForObject = rf.PodSelectorForObject
f.MapBasedSelectorForObject = rf.MapBasedSelectorForObject
f.PortsForObject = rf.PortsForObject
f.LabelsForObject = rf.LabelsForObject
f.CanBeExposed = rf.CanBeExposed

View File

@@ -149,9 +149,9 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
// For objects that need a pod selector, derive it from the exposed object in case a user
// didn't explicitly specify one via --selector
if s, found := params["selector"]; found && kubectl.IsZero(s) {
s, err := f.PodSelectorForObject(inputObject)
s, err := f.MapBasedSelectorForObject(inputObject)
if err != nil {
return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find selectors via --selector flag or introspection: %s", err))
return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't retrieve selectors via --selector flag or introspection: %s", err))
}
params["selector"] = s
}

View File

@@ -94,6 +94,10 @@ type Factory struct {
Rollbacker func(mapping *meta.RESTMapping) (kubectl.Rollbacker, error)
// PodSelectorForObject returns the pod selector associated with the provided object
PodSelectorForObject func(object runtime.Object) (string, error)
// MapBasedSelectorForObject returns the map-based selector associated with the provided object. If a
// new set-based selector is provided, an error is returned if the selector cannot be converted to a
// map-based selector
MapBasedSelectorForObject func(object runtime.Object) (string, error)
// PortsForObject returns the ports associated with the provided object
PortsForObject func(object runtime.Object) ([]string, error)
// LabelsForObject returns the labels associated with the provided object
@@ -257,7 +261,41 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return kubectl.MakeLabels(t.Spec.Selector), nil
case *extensions.Deployment:
selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector)
if err != nil {
return "", fmt.Errorf("failed to convert label selector to selector: %v", err)
}
return selector.String(), nil
default:
gvk, err := api.Scheme.ObjectKind(object)
if err != nil {
return "", err
}
return "", fmt.Errorf("cannot extract pod selector from %v", gvk)
}
},
MapBasedSelectorForObject: func(object runtime.Object) (string, error) {
// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
switch t := object.(type) {
case *api.ReplicationController:
return kubectl.MakeLabels(t.Spec.Selector), nil
case *api.Pod:
if len(t.Labels) == 0 {
return "", fmt.Errorf("the pod has no labels and cannot be exposed")
}
return kubectl.MakeLabels(t.Labels), nil
case *api.Service:
if t.Spec.Selector == nil {
return "", fmt.Errorf("the service has no pod selector set")
}
return kubectl.MakeLabels(t.Spec.Selector), nil
case *extensions.Deployment:
// TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
// operator, DoubleEquals operator and In operator with only one element in the set.
if len(t.Spec.Selector.MatchExpressions) > 0 {
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format")
}
return kubectl.MakeLabels(t.Spec.Selector.MatchLabels), nil
default:
gvk, err := api.Scheme.ObjectKind(object)
if err != nil {
@@ -450,13 +488,13 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
selector := labels.SelectorFromSet(t.Spec.Selector)
return GetFirstPod(client, t.Namespace, selector)
case *extensions.Deployment:
selector, err := extensions.LabelSelectorAsSelector(t.Spec.Selector)
selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("failed to convert label selector to selector: %v", err)
}
return GetFirstPod(client, t.Namespace, selector)
case *extensions.Job:
selector, err := extensions.LabelSelectorAsSelector(t.Spec.Selector)
selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("failed to convert label selector to selector: %v", err)
}

View File

@@ -67,11 +67,11 @@ func (h *DeploymentHistoryViewer) History(namespace, name string) (HistoryInfo,
if err != nil {
return historyInfo, fmt.Errorf("failed to retrieve deployment %s: %v", name, err)
}
_, allOldRCs, err := deploymentutil.GetOldRCs(*deployment, h.c)
_, allOldRCs, err := deploymentutil.GetOldReplicaSets(*deployment, h.c)
if err != nil {
return historyInfo, fmt.Errorf("failed to retrieve old RCs from deployment %s: %v", name, err)
}
newRC, err := deploymentutil.GetNewRC(*deployment, h.c)
newRC, err := deploymentutil.GetNewReplicaSet(*deployment, h.c)
if err != nil {
return historyInfo, fmt.Errorf("failed to retrieve new RC from deployment %s: %v", name, err)
}

View File

@@ -103,7 +103,7 @@ func (DeploymentV1Beta1) Generate(genericParams map[string]interface{}) (runtime
},
Spec: extensions.DeploymentSpec{
Replicas: count,
Selector: &extensions.LabelSelector{MatchLabels: labels},
Selector: &unversioned.LabelSelector{MatchLabels: labels},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: labels,

View File

@@ -656,7 +656,7 @@ func TestGenerateDeployment(t *testing.T) {
},
Spec: extensions.DeploymentSpec{
Replicas: 3,
Selector: &extensions.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"foo": "bar", "baz": "blah"},

View File

@@ -48,8 +48,9 @@ func ScalerFor(kind unversioned.GroupKind, c client.Interface) (Scaler, error) {
return &ReplicaSetScaler{c.Extensions()}, nil
case extensions.Kind("Job"):
return &JobScaler{c.Extensions()}, nil
case extensions.Kind("Deployment"):
return &DeploymentScaler{c.Extensions()}, nil
// TODO(madhusudancs): Fix this when Scale group issues are resolved.
// case extensions.Kind("Deployment"):
// return &DeploymentScaler{c.Extensions()}, nil
}
return nil, fmt.Errorf("no scaler has been implemented for %q", kind)
}
@@ -327,56 +328,57 @@ func (precondition *ScalePrecondition) ValidateDeployment(deployment *extensions
return nil
}
type DeploymentScaler struct {
c client.ExtensionsInterface
}
// TODO(madhusudancs): Fix this when Scale group issues are resolved.
// type DeploymentScaler struct {
// c client.ExtensionsInterface
// }
// ScaleSimple is responsible for updating a deployment's desired replicas count.
func (scaler *DeploymentScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint) error {
deployment, err := scaler.c.Deployments(namespace).Get(name)
if err != nil {
return ScaleError{ScaleGetFailure, "Unknown", err}
}
if preconditions != nil {
if err := preconditions.ValidateDeployment(deployment); err != nil {
return err
}
}
scale, err := extensions.ScaleFromDeployment(deployment)
if err != nil {
return ScaleError{ScaleUpdateFailure, deployment.ResourceVersion, err}
}
scale.Spec.Replicas = int(newSize)
if _, err := scaler.c.Scales(namespace).Update("Deployment", scale); err != nil {
if errors.IsInvalid(err) {
return ScaleError{ScaleUpdateInvalidFailure, deployment.ResourceVersion, err}
}
return ScaleError{ScaleUpdateFailure, deployment.ResourceVersion, err}
}
return nil
}
// // ScaleSimple is responsible for updating a deployment's desired replicas count.
// func (scaler *DeploymentScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint) error {
// deployment, err := scaler.c.Deployments(namespace).Get(name)
// if err != nil {
// return ScaleError{ScaleGetFailure, "Unknown", err}
// }
// if preconditions != nil {
// if err := preconditions.ValidateDeployment(deployment); err != nil {
// return err
// }
// }
// scale, err := extensions.ScaleFromDeployment(deployment)
// if err != nil {
// return ScaleError{ScaleUpdateFailure, deployment.ResourceVersion, err}
// }
// scale.Spec.Replicas = int(newSize)
// if _, err := scaler.c.Scales(namespace).Update("Deployment", scale); err != nil {
// if errors.IsInvalid(err) {
// return ScaleError{ScaleUpdateInvalidFailure, deployment.ResourceVersion, err}
// }
// return ScaleError{ScaleUpdateFailure, deployment.ResourceVersion, err}
// }
// return nil
// }
// Scale updates a deployment to a new size, with optional precondition check (if preconditions is not nil),
// optional retries (if retry is not nil), and then optionally waits for the status to reach desired count.
func (scaler *DeploymentScaler) Scale(namespace, name string, newSize uint, preconditions *ScalePrecondition, retry, waitForReplicas *RetryParams) error {
if preconditions == nil {
preconditions = &ScalePrecondition{-1, ""}
}
if retry == nil {
// Make it try only once, immediately
retry = &RetryParams{Interval: time.Millisecond, Timeout: time.Millisecond}
}
cond := ScaleCondition(scaler, preconditions, namespace, name, newSize)
if err := wait.Poll(retry.Interval, retry.Timeout, cond); err != nil {
return err
}
if waitForReplicas != nil {
deployment, err := scaler.c.Deployments(namespace).Get(name)
if err != nil {
return err
}
return wait.Poll(waitForReplicas.Interval, waitForReplicas.Timeout,
client.DeploymentHasDesiredReplicas(scaler.c, deployment))
}
return nil
}
// // Scale updates a deployment to a new size, with optional precondition check (if preconditions is not nil),
// // optional retries (if retry is not nil), and then optionally waits for the status to reach desired count.
// func (scaler *DeploymentScaler) Scale(namespace, name string, newSize uint, preconditions *ScalePrecondition, retry, waitForReplicas *RetryParams) error {
// if preconditions == nil {
// preconditions = &ScalePrecondition{-1, ""}
// }
// if retry == nil {
// // Make it try only once, immediately
// retry = &RetryParams{Interval: time.Millisecond, Timeout: time.Millisecond}
// }
// cond := ScaleCondition(scaler, preconditions, namespace, name, newSize)
// if err := wait.Poll(retry.Interval, retry.Timeout, cond); err != nil {
// return err
// }
// if waitForReplicas != nil {
// deployment, err := scaler.c.Deployments(namespace).Get(name)
// if err != nil {
// return err
// }
// return wait.Poll(waitForReplicas.Interval, waitForReplicas.Timeout,
// client.DeploymentHasDesiredReplicas(scaler.c, deployment))
// }
// return nil
// }

View File

@@ -488,143 +488,145 @@ func TestValidateJob(t *testing.T) {
}
}
type ErrorScales struct {
testclient.FakeScales
invalid bool
}
// TODO(madhusudancs): Fix this when Scale group issues are resolved.
func (c *ErrorScales) Update(kind string, scale *extensions.Scale) (*extensions.Scale, error) {
if c.invalid {
return nil, kerrors.NewInvalid(extensions.Kind(scale.Kind), scale.Name, nil)
}
return nil, errors.New("scale update failure")
}
// type ErrorScales struct {
// testclient.FakeScales
// invalid bool
// }
func (c *ErrorScales) Get(kind, name string) (*extensions.Scale, error) {
return &extensions.Scale{
Spec: extensions.ScaleSpec{
Replicas: 0,
},
}, nil
}
// func (c *ErrorScales) Update(kind string, scale *extensions.Scale) (*extensions.Scale, error) {
// if c.invalid {
// return nil, kerrors.NewInvalid(extensions.Kind(scale.Kind), scale.Name, nil)
// }
// return nil, errors.New("scale update failure")
// }
type ErrorDeployments struct {
testclient.FakeDeployments
invalid bool
}
// func (c *ErrorScales) Get(kind, name string) (*extensions.Scale, error) {
// return &extensions.Scale{
// Spec: extensions.ScaleSpec{
// Replicas: 0,
// },
// }, nil
// }
func (c *ErrorDeployments) Update(deployment *extensions.Deployment) (*extensions.Deployment, error) {
if c.invalid {
return nil, kerrors.NewInvalid(extensions.Kind(deployment.Kind), deployment.Name, nil)
}
return nil, errors.New("deployment update failure")
}
// type ErrorDeployments struct {
// testclient.FakeDeployments
// invalid bool
// }
func (c *ErrorDeployments) Get(name string) (*extensions.Deployment, error) {
return &extensions.Deployment{
Spec: extensions.DeploymentSpec{
Replicas: 0,
},
}, nil
}
// func (c *ErrorDeployments) Update(deployment *extensions.Deployment) (*extensions.Deployment, error) {
// if c.invalid {
// return nil, kerrors.NewInvalid(extensions.Kind(deployment.Kind), deployment.Name, nil)
// }
// return nil, errors.New("deployment update failure")
// }
type ErrorDeploymentClient struct {
testclient.FakeExperimental
invalid bool
}
// func (c *ErrorDeployments) Get(name string) (*extensions.Deployment, error) {
// return &extensions.Deployment{
// Spec: extensions.DeploymentSpec{
// Replicas: 0,
// },
// }, nil
// }
func (c *ErrorDeploymentClient) Deployments(namespace string) client.DeploymentInterface {
return &ErrorDeployments{testclient.FakeDeployments{Fake: &c.FakeExperimental, Namespace: namespace}, c.invalid}
}
// type ErrorDeploymentClient struct {
// testclient.FakeExperimental
// invalid bool
// }
func (c *ErrorDeploymentClient) Scales(namespace string) client.ScaleInterface {
return &ErrorScales{testclient.FakeScales{Fake: &c.FakeExperimental, Namespace: namespace}, c.invalid}
}
// func (c *ErrorDeploymentClient) Deployments(namespace string) client.DeploymentInterface {
// return &ErrorDeployments{testclient.FakeDeployments{Fake: &c.FakeExperimental, Namespace: namespace}, c.invalid}
// }
func TestDeploymentScaleRetry(t *testing.T) {
fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{Fake: &testclient.Fake{}}, invalid: false}
scaler := &DeploymentScaler{fake}
preconditions := &ScalePrecondition{-1, ""}
count := uint(3)
name := "foo"
namespace := "default"
// func (c *ErrorDeploymentClient) Scales(namespace string) client.ScaleInterface {
// return &ErrorScales{testclient.FakeScales{Fake: &c.FakeExperimental, Namespace: namespace}, c.invalid}
// }
scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count)
pass, err := scaleFunc()
if pass != false {
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
}
if err != nil {
t.Errorf("Did not expect an error on update failure, got %v", err)
}
preconditions = &ScalePrecondition{3, ""}
scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count)
pass, err = scaleFunc()
if err == nil {
t.Errorf("Expected error on precondition failure")
}
}
// func TestDeploymentScaleRetry(t *testing.T) {
// fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{Fake: &testclient.Fake{}}, invalid: false}
// scaler := &DeploymentScaler{fake}
// preconditions := &ScalePrecondition{-1, ""}
// count := uint(3)
// name := "foo"
// namespace := "default"
func TestDeploymentScale(t *testing.T) {
fake := &testclient.FakeExperimental{Fake: &testclient.Fake{}}
scaler := DeploymentScaler{fake}
preconditions := ScalePrecondition{-1, ""}
count := uint(3)
name := "foo"
scaler.Scale("default", name, count, &preconditions, nil, nil)
// scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count)
// pass, err := scaleFunc()
// if pass != false {
// t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
// }
// if err != nil {
// t.Errorf("Did not expect an error on update failure, got %v", err)
// }
// preconditions = &ScalePrecondition{3, ""}
// scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count)
// pass, err = scaleFunc()
// if err == nil {
// t.Errorf("Expected error on precondition failure")
// }
// }
actions := fake.Actions()
if len(actions) != 2 {
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
}
if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
}
// TODO: The testclient needs to support subresources
if action, ok := actions[1].(testclient.UpdateAction); !ok || action.GetResource() != "Deployment" || action.GetObject().(*extensions.Scale).Spec.Replicas != int(count) {
t.Errorf("unexpected action %v, expected update-deployment-scale with replicas = %d", actions[1], count)
}
}
// func TestDeploymentScale(t *testing.T) {
// fake := &testclient.FakeExperimental{Fake: &testclient.Fake{}}
// scaler := DeploymentScaler{fake}
// preconditions := ScalePrecondition{-1, ""}
// count := uint(3)
// name := "foo"
// scaler.Scale("default", name, count, &preconditions, nil, nil)
func TestDeploymentScaleInvalid(t *testing.T) {
fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{Fake: &testclient.Fake{}}, invalid: true}
scaler := DeploymentScaler{fake}
preconditions := ScalePrecondition{-1, ""}
count := uint(3)
name := "foo"
namespace := "default"
// actions := fake.Actions()
// if len(actions) != 2 {
// t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
// }
// if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
// t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
// }
// // TODO: The testclient needs to support subresources
// if action, ok := actions[1].(testclient.UpdateAction); !ok || action.GetResource() != "Deployment" || action.GetObject().(*extensions.Scale).Spec.Replicas != int(count) {
// t.Errorf("unexpected action %v, expected update-deployment-scale with replicas = %d", actions[1], count)
// }
// }
scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count)
pass, err := scaleFunc()
if pass {
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
}
e, ok := err.(ScaleError)
if err == nil || !ok || e.FailureType != ScaleUpdateInvalidFailure {
t.Errorf("Expected error on invalid update failure, got %v", err)
}
}
// func TestDeploymentScaleInvalid(t *testing.T) {
// fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{Fake: &testclient.Fake{}}, invalid: true}
// scaler := DeploymentScaler{fake}
// preconditions := ScalePrecondition{-1, ""}
// count := uint(3)
// name := "foo"
// namespace := "default"
func TestDeploymentScaleFailsPreconditions(t *testing.T) {
fake := testclient.NewSimpleFake(&extensions.Deployment{
Spec: extensions.DeploymentSpec{
Replicas: 10,
},
})
scaler := DeploymentScaler{&testclient.FakeExperimental{fake}}
preconditions := ScalePrecondition{2, ""}
count := uint(3)
name := "foo"
scaler.Scale("default", name, count, &preconditions, nil, nil)
// scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count)
// pass, err := scaleFunc()
// if pass {
// t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
// }
// e, ok := err.(ScaleError)
// if err == nil || !ok || e.FailureType != ScaleUpdateInvalidFailure {
// t.Errorf("Expected error on invalid update failure, got %v", err)
// }
// }
actions := fake.Actions()
if len(actions) != 1 {
t.Errorf("unexpected actions: %v, expected 1 actions (get)", actions)
}
if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
t.Errorf("unexpected action: %v, expected get-deployment %s", actions[0], name)
}
}
// func TestDeploymentScaleFailsPreconditions(t *testing.T) {
// fake := testclient.NewSimpleFake(&extensions.Deployment{
// Spec: extensions.DeploymentSpec{
// Replicas: 10,
// },
// })
// scaler := DeploymentScaler{&testclient.FakeExperimental{fake}}
// preconditions := ScalePrecondition{2, ""}
// count := uint(3)
// name := "foo"
// scaler.Scale("default", name, count, &preconditions, nil, nil)
// actions := fake.Actions()
// if len(actions) != 1 {
// t.Errorf("unexpected actions: %v, expected 1 actions (get)", actions)
// }
// if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
// t.Errorf("unexpected action: %v, expected get-deployment %s", actions[0], name)
// }
// }
func TestValidateDeployment(t *testing.T) {
zero, ten, twenty := 0, 10, 20