Skip to content

Handle forbidden fields for statefulsets with forcenew plan fix #84 #88

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

Merged
merged 1 commit into from
Feb 25, 2021
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
20 changes: 16 additions & 4 deletions kustomize/resource_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func kustomizationResource() *schema.Resource {
Expand Down Expand Up @@ -251,10 +252,21 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
if k8serrors.IsInvalid(err) {
as := err.(k8serrors.APIStatus).Status()

// if there is exactly 1 cause and that cause is due to an immutable field force a delete and re-create plan
if len(as.Details.Causes) == 1 && strings.HasSuffix(as.Details.Causes[0].Message, ": field is immutable") == true {
d.ForceNew("manifest")
return nil
// ForceNew only when exact single cause
if len(as.Details.Causes) == 1 {
msg := as.Details.Causes[0].Message

// if cause is immutable field force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseTypeFieldValueInvalid) && strings.HasSuffix(msg, ": field is immutable") == true {
d.ForceNew("manifest")
return nil
}

// if cause is statefulset forbidden fields error force a delete and re-create plan
if k8serrors.HasStatusCause(err, k8smetav1.CauseType(field.ErrorTypeForbidden)) && strings.HasPrefix(msg, "Forbidden: updates to statefulset spec for fields") == true {
d.ForceNew("manifest")
return nil
}
}

// if StrategicMergePatchType fall back to MergePatchType before returning an error to the user
Expand Down
44 changes: 44 additions & 0 deletions kustomize/resource_kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,50 @@ resource "kustomization_resource" "dep1" {
`
}

//
//
// Update_Recreate_StatefulSet Test
func TestAccResourceKustomization_updateRecreateStatefulSet(t *testing.T) {

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
//
//
// Applying initial statefulset
{
Config: testAccResourceKustomizationConfig_updateRecreateStatefulSet("test_kustomizations/update_recreate_statefulset/initial"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"),
resource.TestCheckResourceAttrSet("kustomization_resource.ss", "id"),
),
},
//
//
// Applying modified statefulset that requires a destroy and recreate
{
Config: testAccResourceKustomizationConfig_updateRecreateStatefulSet("test_kustomizations/update_recreate_statefulset/modified"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"),
resource.TestCheckResourceAttrSet("kustomization_resource.ss", "id"),
),
},
},
})
}

func testAccResourceKustomizationConfig_updateRecreateStatefulSet(path string) string {
return testAccDataSourceKustomizationConfig_basic(path) + `
resource "kustomization_resource" "ns" {
manifest = data.kustomization_build.test.manifests["~G_v1_Namespace|~X|test-update-recreate-statefulset"]
}

resource "kustomization_resource" "ss" {
manifest = data.kustomization_build.test.manifests["apps_v1_StatefulSet|test-update-recreate-statefulset|test"]
}
`
}

//
//
// CRD Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: test-update-recreate-statefulset

resources:
- namespace.yaml
- statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: test-update-recreate-statefulset
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: test
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: test-update-recreate-statefulset

resources:
- ../initial

commonLabels:
test-label: "triggers-labelSelector-change"