Skip to content

Commit

Permalink
Merge pull request #56 from form3tech-oss/k8schaos-patch-resource
Browse files Browse the repository at this point in the history
K8schaos `update` option
  • Loading branch information
wvdschel-f3 authored Nov 13, 2023
2 parents 95e3eab + d4492f3 commit aa23dff
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 17 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/k8schaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ type K8SChaosSpec struct {

// +kubebuilder:validation:Required
APIObjects *K8SChaosAPIObjects `json:"apiObjects"`

// Patch specifies that the chaos should update an existing resource rather than create a new one.
// +optional
Update bool `json:"update,omitempty"`

// RemoteCluster represents the remote cluster where the chaos will be deployed
// +optional
RemoteCluster string `json:"remoteCluster,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/chaos-mesh.org_k8schaos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ spec:
description: RemoteCluster represents the remote cluster where the
chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/chaos-mesh.org_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -5031,6 +5035,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -8424,6 +8432,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should
update an existing resource rather than create
a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
18 changes: 18 additions & 0 deletions config/crd/bases/chaos-mesh.org_workflownodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -4618,6 +4622,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -8315,6 +8323,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should
update an existing resource rather than create
a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -11816,6 +11829,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos
should update an existing resource rather
than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/chaos-mesh.org_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -4796,6 +4800,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
34 changes: 28 additions & 6 deletions controllers/chaosimpl/k8schaos/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var _ impltypes.ChaosImpl = (*Impl)(nil)
type Impl struct {
client.Client
Log logr.Logger

initialValue *unstructured.Unstructured
}

const (
Expand Down Expand Up @@ -83,7 +85,20 @@ func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Reco
return v1alpha1.NotInjected, fmt.Errorf("get rest mapping: %w", err)
}

_, err = client.Resource(mapping.Resource).Namespace(resource.GetNamespace()).Create(ctx, resource, v1.CreateOptions{})
if k8schaos.Spec.Update {
impl.initialValue, err = client.Resource(mapping.Resource).Namespace(resource.GetNamespace()).Get(ctx, resource.GetName(), v1.GetOptions{})
if err != nil && !apiErrors.IsNotFound(err) {
return v1alpha1.NotInjected, err
}

unstructured.RemoveNestedField(impl.initialValue.Object, "metadata", "creationTimestamp")
unstructured.RemoveNestedField(impl.initialValue.Object, "metadata", "resourceVersion")
unstructured.RemoveNestedField(impl.initialValue.Object, "metadata", "uid")

_, err = client.Resource(mapping.Resource).Namespace(resource.GetNamespace()).Update(ctx, resource, v1.UpdateOptions{})
} else {
_, err = client.Resource(mapping.Resource).Namespace(resource.GetNamespace()).Create(ctx, resource, v1.CreateOptions{})
}
if err != nil {
return v1alpha1.NotInjected, err
}
Expand Down Expand Up @@ -129,21 +144,28 @@ func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Re
return v1alpha1.Injected, err
}

if !isManaged(existingResource) {
return v1alpha1.Injected, fmt.Errorf("resource is not managed by %s", managedBy)
if resMgr := getResourceManager(existingResource); resMgr != managedBy {
return v1alpha1.Injected, fmt.Errorf("resource is not managed by %s: %s: \"%s\"", managedBy, managedByLabel, resMgr)
}

err = resourceClient.Delete(ctx, resource.GetName(), v1.DeleteOptions{})
if impl.initialValue != nil {
_, err = client.Resource(mapping.Resource).Namespace(resource.GetNamespace()).Update(ctx, impl.initialValue, v1.UpdateOptions{})
} else {
err = resourceClient.Delete(ctx, resource.GetName(), v1.DeleteOptions{})
}
if err != nil && !apiErrors.IsNotFound(err) {
return v1alpha1.Injected, err
}

return v1alpha1.NotInjected, nil
}

func isManaged(resource *unstructured.Unstructured) bool {
func getResourceManager(resource *unstructured.Unstructured) string {
labels := resource.GetLabels()
return labels != nil && labels[managedByLabel] == managedBy
if labels == nil {
return ""
}
return labels[managedByLabel]
}

func (impl *Impl) dynamicClient() (dynamic.Interface, error) {
Expand Down
4 changes: 4 additions & 0 deletions helm/chaos-mesh/crds/chaos-mesh.org_k8schaos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ spec:
description: RemoteCluster represents the remote cluster where the
chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
13 changes: 13 additions & 0 deletions helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -5031,6 +5035,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -8424,6 +8432,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should
update an existing resource rather than create
a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
18 changes: 18 additions & 0 deletions helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update an existing
resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -4618,6 +4622,10 @@ spec:
description: RemoteCluster represents the remote cluster where
the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -8315,6 +8323,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should
update an existing resource rather than create
a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -11816,6 +11829,11 @@ spec:
description: RemoteCluster represents the remote
cluster where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos
should update an existing resource rather
than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
8 changes: 8 additions & 0 deletions helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down Expand Up @@ -4796,6 +4800,10 @@ spec:
description: RemoteCluster represents the remote cluster
where the chaos will be deployed
type: string
update:
description: Patch specifies that the chaos should update
an existing resource rather than create a new one.
type: boolean
required:
- apiObjects
type: object
Expand Down
Loading

0 comments on commit aa23dff

Please sign in to comment.