-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds command and client for the
clusterctl alpha rollout pause/resume
for MachineDeployments.
- Loading branch information
1 parent
daba8fe
commit b769eab
Showing
14 changed files
with
711 additions
and
81 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 alpha | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// ObjectPauser will issue a pause on the specified cluster-api resource. | ||
func (r *rollout) ObjectPauser(proxy cluster.Proxy, tuple util.ResourceTuple, namespace string) error { | ||
switch tuple.Resource { | ||
case machineDeployment: | ||
deployment, err := getMachineDeployment(proxy, tuple.Name, namespace) | ||
if err != nil || deployment == nil { | ||
return errors.Wrapf(err, "failed to fetch %v/%v", tuple.Resource, tuple.Name) | ||
} | ||
if deployment.Spec.Paused { | ||
return errors.Errorf("MachineDeploymet is already paused: %v/%v\n", tuple.Resource, tuple.Name) | ||
} | ||
if err := pauseMachineDeployment(proxy, tuple.Name, namespace); err != nil { | ||
return err | ||
} | ||
default: | ||
return errors.Errorf("Invalid resource type %q, valid values are %v", tuple.Resource, validResourceTypes) | ||
} | ||
return nil | ||
} | ||
|
||
// pauseMachineDeployment sets Paused to true in the MachineDeployment's spec. | ||
func pauseMachineDeployment(proxy cluster.Proxy, name, namespace string) error { | ||
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", true))) | ||
return patchMachineDeployemt(proxy, name, namespace, patch) | ||
} |
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,115 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 alpha | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func Test_ObjectPauser(t *testing.T) { | ||
type fields struct { | ||
objs []client.Object | ||
tuple util.ResourceTuple | ||
namespace string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
wantPaused bool | ||
}{ | ||
{ | ||
name: "machinedeployment should be paused", | ||
fields: fields{ | ||
objs: []client.Object{ | ||
&clusterv1.MachineDeployment{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "MachineDeployment", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "md-1", | ||
}, | ||
}, | ||
}, | ||
tuple: util.ResourceTuple{ | ||
Resource: "machinedeployment", | ||
Name: "md-1", | ||
}, | ||
namespace: "default", | ||
}, | ||
wantErr: false, | ||
wantPaused: true, | ||
}, | ||
{ | ||
name: "re-pausing an already paused machinedeployment should return error", | ||
fields: fields{ | ||
objs: []client.Object{ | ||
&clusterv1.MachineDeployment{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "MachineDeployment", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "md-1", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Paused: true, | ||
}, | ||
}, | ||
}, | ||
tuple: util.ResourceTuple{ | ||
Resource: "machinedeployment", | ||
Name: "md-1", | ||
}, | ||
namespace: "default", | ||
}, | ||
wantErr: true, | ||
wantPaused: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
r := newRolloutClient() | ||
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...) | ||
err := r.ObjectPauser(proxy, tt.fields.tuple, tt.fields.namespace) | ||
if tt.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
return | ||
} | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
for _, obj := range tt.fields.objs { | ||
cl, err := proxy.NewClient() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
key := client.ObjectKeyFromObject(obj) | ||
md := &clusterv1.MachineDeployment{} | ||
err = cl.Get(context.TODO(), key, md) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused)) | ||
} | ||
}) | ||
} | ||
} |
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,54 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 alpha | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// ObjectResumer will issue a resume on the specified cluster-api resource. | ||
func (r *rollout) ObjectResumer(proxy cluster.Proxy, tuple util.ResourceTuple, namespace string) error { | ||
switch tuple.Resource { | ||
case "machinedeployment": | ||
deployment, err := getMachineDeployment(proxy, tuple.Name, namespace) | ||
if err != nil || deployment == nil { | ||
return errors.Wrapf(err, "failed to fetch %v/%v", tuple.Resource, tuple.Name) | ||
} | ||
if !deployment.Spec.Paused { | ||
return errors.Errorf("MachineDeployment is not currently paused: %v/%v\n", tuple.Resource, tuple.Name) | ||
} | ||
if err := resumeMachineDeployment(proxy, tuple.Name, namespace); err != nil { | ||
return err | ||
} | ||
default: | ||
return errors.Errorf("Invalid resource type %q, valid values are %v", tuple.Resource, validResourceTypes) | ||
} | ||
return nil | ||
} | ||
|
||
// resumeMachineDeployment sets Paused to true in the MachineDeployment's spec. | ||
func resumeMachineDeployment(proxy cluster.Proxy, name, namespace string) error { | ||
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", false))) | ||
|
||
return patchMachineDeployemt(proxy, name, namespace, patch) | ||
} |
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,118 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 alpha | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func Test_ObjectResumer(t *testing.T) { | ||
type fields struct { | ||
objs []client.Object | ||
tuple util.ResourceTuple | ||
namespace string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
wantPaused bool | ||
}{ | ||
{ | ||
name: "paused machinedeployment should be unpaused", | ||
fields: fields{ | ||
objs: []client.Object{ | ||
&clusterv1.MachineDeployment{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "MachineDeployment", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "md-1", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Paused: true, | ||
}, | ||
}, | ||
}, | ||
tuple: util.ResourceTuple{ | ||
Resource: "machinedeployment", | ||
Name: "md-1", | ||
}, | ||
namespace: "default", | ||
}, | ||
wantErr: false, | ||
wantPaused: false, | ||
}, | ||
{ | ||
name: "unpausing an already unpaused machinedeployment should return error", | ||
fields: fields{ | ||
objs: []client.Object{ | ||
&clusterv1.MachineDeployment{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "MachineDeployment", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "md-1", | ||
}, | ||
Spec: clusterv1.MachineDeploymentSpec{ | ||
Paused: false, | ||
}, | ||
}, | ||
}, | ||
tuple: util.ResourceTuple{ | ||
Resource: "machinedeployment", | ||
Name: "md-1", | ||
}, | ||
namespace: "default", | ||
}, | ||
wantErr: true, | ||
wantPaused: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
r := newRolloutClient() | ||
proxy := test.NewFakeProxy().WithObjs(tt.fields.objs...) | ||
err := r.ObjectResumer(proxy, tt.fields.tuple, tt.fields.namespace) | ||
if tt.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
return | ||
} | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
for _, obj := range tt.fields.objs { | ||
cl, err := proxy.NewClient() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
key := client.ObjectKeyFromObject(obj) | ||
md := &clusterv1.MachineDeployment{} | ||
err = cl.Get(context.TODO(), key, md) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(md.Spec.Paused).To(Equal(tt.wantPaused)) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.