Skip to content

Commit

Permalink
Add command and client for clusterctl alpha rollout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvinderpal committed Dec 9, 2020
1 parent e6b0039 commit e2c12b5
Show file tree
Hide file tree
Showing 16 changed files with 975 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cmd/clusterctl/client/alpha/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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

// Client is the alpha client
type Client interface {
Rollout() Rollout
}

// alphaClient implements Client.
type alphaClient struct {
rollout Rollout
}

// ensure alphaClient implements Client.
var _ Client = &alphaClient{}

// Option is a configuration option supplied to New
type Option func(*alphaClient)

// InjectRollout allows to override the rollout implementation to use.
func InjectRollout(rollout Rollout) Option {
return func(c *alphaClient) {
c.rollout = rollout
}
}

// New returns a Client.
func New(options ...Option) Client {
return newAlphaClient(options...)
}

func newAlphaClient(options ...Option) *alphaClient {
client := &alphaClient{}
for _, o := range options {
o(client)
}

// if there is an injected rollout, use it, otherwise use a default one
if client.rollout == nil {
client.rollout = newRolloutClient()
}

return client
}

func (c *alphaClient) Rollout() Rollout {
return c.rollout
}
35 changes: 35 additions & 0 deletions cmd/clusterctl/client/alpha/rollout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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 (
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
)

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

var _ Rollout = &rollout{}

type rollout struct{}

func newRolloutClient() Rollout {
return &rollout{}
}
97 changes: 97 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_restarter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
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"
"fmt"
"time"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"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"
)

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 {
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("can't restart paused machinedeployment (run rollout resume first): %v/%v\n", tuple.Resource, tuple.Name)
}
if err := setRestartedAtAnnotation(proxy, tuple.Name, namespace); err != nil {
return err
}
default:
return errors.Errorf("Invalid resource type %v. Valid values: %v", tuple.Resource, validResourceTypes)
}
return nil
}

// getMachineDeployment retrieves the MachineDeployment object corresponding to the name and namespace specified.
func getMachineDeployment(proxy cluster.Proxy, name, namespace string) (*clusterv1.MachineDeployment, error) {
mdObj := &clusterv1.MachineDeployment{}
c, err := proxy.NewClient()
if err != nil {
return nil, err
}
mdObjKey := client.ObjectKey{
Namespace: namespace,
Name: name,
}
if err := c.Get(context.TODO(), mdObjKey, mdObj); err != nil {
return nil, errors.Wrapf(err, "error reading %q %s/%s",
mdObj.GroupVersionKind(), mdObjKey.Namespace, mdObjKey.Name)
}
return mdObj, nil
}

// setRestartedAtAnnotation sets the restartedAt annotation in the MachineDeployment's spec.template.objectmeta.
func setRestartedAtAnnotation(proxy cluster.Proxy, name, namespace string) error {
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"cluster.x-k8s.io/restartedAt\":\"%v\"}}}}}", time.Now().Format(time.RFC3339))))
return patchMachineDeployemt(proxy, name, namespace, patch)
}

// patchMachineDeployemt applies a patch to a machinedeployment
func patchMachineDeployemt(proxy cluster.Proxy, name, namespace string, patch client.Patch) error {
cFrom, err := proxy.NewClient()
if err != nil {
return err
}
mdObj := &clusterv1.MachineDeployment{}
mdObjKey := client.ObjectKey{
Namespace: namespace,
Name: name,
}
if err := cFrom.Get(context.TODO(), mdObjKey, mdObj); err != nil {
return errors.Wrapf(err, "error reading %s/%s", mdObj.GetNamespace(), mdObj.GetName())
}

if err := cFrom.Patch(context.TODO(), mdObj, patch); err != nil {
return errors.Wrapf(err, "error while patching %s/%s", mdObj.GetNamespace(), mdObj.GetName())
}
return nil
}
122 changes: 122 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_restarter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
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_ObjectRestarter(t *testing.T) {
type fields struct {
objs []client.Object
tuple util.ResourceTuple
namespace string
}
tests := []struct {
name string
fields fields
wantErr bool
wantAnnotation bool
}{
{
name: "machinedeployment should have restart annotation",
fields: fields{
objs: []client.Object{
&clusterv1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
APIVersion: "cluster.x-k8s.io/v1alpha4",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "md-1",
},
},
},
tuple: util.ResourceTuple{
Resource: "machinedeployment",
Name: "md-1",
},
namespace: "default",
},
wantErr: false,
wantAnnotation: true,
},
{
name: "paused machinedeployment should not have restart annotation",
fields: fields{
objs: []client.Object{
&clusterv1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
APIVersion: "cluster.x-k8s.io/v1alpha4",
},
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,
wantAnnotation: 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.ObjectRestarter(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())
if tt.wantAnnotation {
g.Expect(md.Spec.Template.Annotations).To(HaveKey("cluster.x-k8s.io/restartedAt"))
} else {
g.Expect(md.Spec.Template.Annotations).ToNot(HaveKey("cluster.x-k8s.io/restartedAt"))
}

}
})
}
}
17 changes: 17 additions & 0 deletions cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client

import (
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/alpha"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
Expand Down Expand Up @@ -65,6 +66,15 @@ type Client interface {
// ProcessYAML provides a direct way to process a yaml and inspect its
// variables.
ProcessYAML(options ProcessYAMLOptions) (YamlPrinter, error)

// Interface for alpha features in clusterctl
AlphaClient
}

// 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
}

// YamlPrinter exposes methods that prints the processed template and
Expand All @@ -82,6 +92,7 @@ type clusterctlClient struct {
configClient config.Client
repositoryClientFactory RepositoryClientFactory
clusterClientFactory ClusterClientFactory
alphaClient alpha.Client
}

// RepositoryClientFactoryInput represents the inputs required by the
Expand Down Expand Up @@ -160,6 +171,12 @@ func newClusterctlClient(path string, options ...Option) (*clusterctlClient, err
client.clusterClientFactory = defaultClusterFactory(client.configClient)
}

// if there is an injected alphaClient, use it, otherwise use a default one.
if client.alphaClient == nil {
c := alpha.New()
client.alphaClient = c
}

return client, nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (f fakeClient) ProcessYAML(options ProcessYAMLOptions) (YamlPrinter, error)
return f.internalClient.ProcessYAML(options)
}

func (f fakeClient) RolloutRestart(options RolloutRestartOptions) error {
return f.internalClient.RolloutRestart(options)
}

// newFakeClient returns a clusterctl client that allows to execute tests on a set of fake config, fake repositories and fake clusters.
// you can use WithCluster and WithRepository to prepare for the test case.
func newFakeClient(configClient config.Client) *fakeClient {
Expand Down
Loading

0 comments on commit e2c12b5

Please sign in to comment.