Skip to content
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

✨ clusterctl alpha rollout pause/resume for MachineDeployments #4054

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/clusterctl/client/alpha/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ import (
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
)

const machineDeployment = "machinedeployment"

var validResourceTypes = []string{machineDeployment}

// Rollout defines the behavior of a rollout implementation.
type Rollout interface {
ObjectRestarter(cluster.Proxy, util.ResourceTuple, string) error
ObjectPauser(cluster.Proxy, util.ResourceTuple, string) error
ObjectResumer(cluster.Proxy, util.ResourceTuple, string) error
}

var _ Rollout = &rollout{}
Expand Down
53 changes: 53 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_pauser.go
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)
}
115 changes: 115 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_pauser_test.go
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))
}
})
}
}
2 changes: 0 additions & 2 deletions cmd/clusterctl/client/alpha/rollout_restarter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var validResourceTypes = []string{"machinedeployment"}

// ObjectRestarter will issue a restart on the specified cluster-api resource.
func (r *rollout) ObjectRestarter(proxy cluster.Proxy, tuple util.ResourceTuple, namespace string) error {
switch tuple.Resource {
Expand Down
54 changes: 54 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_resumer.go
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)
}
118 changes: 118 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_resumer_test.go
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))
}
})
}
}
6 changes: 5 additions & 1 deletion cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ type Client interface {
// AlphaClient exposes the alpha features in clusterctl high-level client library.
type AlphaClient interface {
// RolloutRestart provides rollout restart of cluster-api resources
RolloutRestart(options RolloutRestartOptions) error
RolloutRestart(options RolloutOptions) error
// RolloutPause provides rollout pause of cluster-api resources
RolloutPause(options RolloutOptions) error
// RolloutResume provides rollout resume of paused cluster-api resources
RolloutResume(options RolloutOptions) error
}

// YamlPrinter exposes methods that prints the processed template and
Expand Down
Loading