-
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.
Add command and client for
clusterctl alpha rollout
.
- Loading branch information
1 parent
e6b0039
commit e2c12b5
Showing
16 changed files
with
975 additions
and
0 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
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 | ||
} |
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,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{} | ||
} |
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,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 | ||
} |
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,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")) | ||
} | ||
|
||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.