|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package code |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + |
| 20 | + ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/config" |
| 21 | + "github.com/aws-controllers-k8s/code-generator/pkg/model" |
| 22 | +) |
| 23 | + |
| 24 | +// defaultRequeueAfterSeconds is the default delay in seconds before a requeue |
| 25 | +// when a resource is not in an allowed state for update or delete. |
| 26 | +const defaultRequeueAfterSeconds = 30 |
| 27 | + |
| 28 | +// ResourceIsUpdateable returns Go code that checks whether a resource can be |
| 29 | +// updated based on its current status. If the resource is NOT updateable, the |
| 30 | +// generated code returns ackrequeue.NeededAfter. |
| 31 | +// |
| 32 | +// This follows the same pattern as ResourceIsSynced in synced.go. |
| 33 | +// |
| 34 | +// Sample output: |
| 35 | +// |
| 36 | +// if latest.ko.Status.Status != nil { |
| 37 | +// if !ackutil.InStrings(*latest.ko.Status.Status, []string{"ACTIVE", "AVAILABLE"}) { |
| 38 | +// return nil, ackrequeue.NeededAfter( |
| 39 | +// fmt.Errorf("resource is in %s state, cannot be updated", |
| 40 | +// *latest.ko.Status.Status), |
| 41 | +// time.Duration(30)*time.Second, |
| 42 | +// ) |
| 43 | +// } |
| 44 | +// } |
| 45 | +func ResourceIsUpdateable( |
| 46 | + cfg *ackgenconfig.Config, |
| 47 | + r *model.CRD, |
| 48 | + // resource variable name — "latest" for sdkUpdate |
| 49 | + resVarName string, |
| 50 | + // Number of levels of indentation to use |
| 51 | + indentLevel int, |
| 52 | +) (string, error) { |
| 53 | + return resourceIsGuarded(cfg, r, resVarName, indentLevel, "updateable", "updated") |
| 54 | +} |
| 55 | + |
| 56 | +// ResourceIsDeletable returns Go code that checks whether a resource can be |
| 57 | +// deleted based on its current status. If the resource is NOT deletable, the |
| 58 | +// generated code returns ackrequeue.NeededAfter. |
| 59 | +// |
| 60 | +// Sample output: |
| 61 | +// |
| 62 | +// if r.ko.Status.Status != nil { |
| 63 | +// if !ackutil.InStrings(*r.ko.Status.Status, []string{"ACTIVE", "AVAILABLE", "FAILED"}) { |
| 64 | +// return nil, ackrequeue.NeededAfter( |
| 65 | +// fmt.Errorf("resource is in %s state, cannot be deleted", |
| 66 | +// *r.ko.Status.Status), |
| 67 | +// time.Duration(30)*time.Second, |
| 68 | +// ) |
| 69 | +// } |
| 70 | +// } |
| 71 | +func ResourceIsDeletable( |
| 72 | + cfg *ackgenconfig.Config, |
| 73 | + r *model.CRD, |
| 74 | + // resource variable name — "r" for sdkDelete |
| 75 | + resVarName string, |
| 76 | + // Number of levels of indentation to use |
| 77 | + indentLevel int, |
| 78 | +) (string, error) { |
| 79 | + return resourceIsGuarded(cfg, r, resVarName, indentLevel, "deletable", "deleted") |
| 80 | +} |
| 81 | + |
| 82 | +// resourceIsGuarded is the shared implementation for ResourceIsUpdateable and |
| 83 | +// ResourceIsDeletable. It reads the appropriate config block and generates |
| 84 | +// guard code for each condition. |
| 85 | +func resourceIsGuarded( |
| 86 | + cfg *ackgenconfig.Config, |
| 87 | + r *model.CRD, |
| 88 | + resVarName string, |
| 89 | + indentLevel int, |
| 90 | + // configKey is "updateable" or "deletable" |
| 91 | + configKey string, |
| 92 | + // opVerb is "updated" or "deleted" — used in the error message |
| 93 | + opVerb string, |
| 94 | +) (string, error) { |
| 95 | + out := "" |
| 96 | + resConfig := cfg.GetResourceConfig(r.Names.Original) |
| 97 | + if resConfig == nil { |
| 98 | + return out, nil |
| 99 | + } |
| 100 | + |
| 101 | + var conditions []ackgenconfig.StatusCondition |
| 102 | + var requeueSeconds int |
| 103 | + |
| 104 | + switch configKey { |
| 105 | + case "updateable": |
| 106 | + if resConfig.Updateable == nil || len(resConfig.Updateable.When) == 0 { |
| 107 | + return out, nil |
| 108 | + } |
| 109 | + conditions = resConfig.Updateable.When |
| 110 | + requeueSeconds = defaultRequeueAfterSeconds |
| 111 | + if resConfig.Updateable.RequeueAfterSeconds != nil && |
| 112 | + *resConfig.Updateable.RequeueAfterSeconds > 0 { |
| 113 | + requeueSeconds = *resConfig.Updateable.RequeueAfterSeconds |
| 114 | + } |
| 115 | + case "deletable": |
| 116 | + if resConfig.Deletable == nil || len(resConfig.Deletable.When) == 0 { |
| 117 | + return out, nil |
| 118 | + } |
| 119 | + conditions = resConfig.Deletable.When |
| 120 | + requeueSeconds = defaultRequeueAfterSeconds |
| 121 | + if resConfig.Deletable.RequeueAfterSeconds != nil && |
| 122 | + *resConfig.Deletable.RequeueAfterSeconds > 0 { |
| 123 | + requeueSeconds = *resConfig.Deletable.RequeueAfterSeconds |
| 124 | + } |
| 125 | + default: |
| 126 | + return "", fmt.Errorf("unknown config key %q", configKey) |
| 127 | + } |
| 128 | + |
| 129 | + for _, condCfg := range conditions { |
| 130 | + if condCfg.Path == nil || *condCfg.Path == "" { |
| 131 | + return "", fmt.Errorf( |
| 132 | + "resource %q: %s.when condition has empty path", |
| 133 | + r.Names.Original, configKey, |
| 134 | + ) |
| 135 | + } |
| 136 | + if len(condCfg.In) == 0 { |
| 137 | + return "", fmt.Errorf( |
| 138 | + "resource %q, path %q: %s.when condition 'in' must not be empty", |
| 139 | + r.Names.Original, *condCfg.Path, configKey, |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + _, err := getTopLevelField(r, *condCfg.Path) |
| 144 | + if err != nil { |
| 145 | + return "", fmt.Errorf( |
| 146 | + "resource %q: cannot find field for path %q: %w", |
| 147 | + r.Names.Original, *condCfg.Path, err, |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + out += renderGuardBlock( |
| 152 | + resVarName, *condCfg.Path, condCfg.In, |
| 153 | + requeueSeconds, opVerb, indentLevel, |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + return out, nil |
| 158 | +} |
| 159 | + |
| 160 | +// renderGuardBlock produces the Go source code for a single condition check. |
| 161 | +// It generates a nil check on the field pointer, then an InStrings check |
| 162 | +// against the allowed values, returning ackrequeue.NeededAfter if the value |
| 163 | +// is not in the allowed set. |
| 164 | +func renderGuardBlock( |
| 165 | + resVarName string, |
| 166 | + fieldPath string, |
| 167 | + allowedValues []string, |
| 168 | + requeueSeconds int, |
| 169 | + opVerb string, |
| 170 | + indentLevel int, |
| 171 | +) string { |
| 172 | + indent := strings.Repeat("\t", indentLevel) |
| 173 | + fullPath := fmt.Sprintf("%s.ko.%s", resVarName, fieldPath) |
| 174 | + |
| 175 | + valuesSlice := fmt.Sprintf(`[]string{"%s"}`, strings.Join(allowedValues, `", "`)) |
| 176 | + |
| 177 | + out := "" |
| 178 | + out += fmt.Sprintf("%sif %s != nil {\n", indent, fullPath) |
| 179 | + out += fmt.Sprintf("%s\tif !ackutil.InStrings(*%s, %s) {\n", indent, fullPath, valuesSlice) |
| 180 | + out += fmt.Sprintf("%s\t\treturn nil, ackrequeue.NeededAfter(\n", indent) |
| 181 | + out += fmt.Sprintf("%s\t\t\tfmt.Errorf(\"resource is in %%s state, cannot be %s\",\n", indent, opVerb) |
| 182 | + out += fmt.Sprintf("%s\t\t\t\t*%s),\n", indent, fullPath) |
| 183 | + out += fmt.Sprintf("%s\t\t\ttime.Duration(%d)*time.Second,\n", indent, requeueSeconds) |
| 184 | + out += fmt.Sprintf("%s\t\t)\n", indent) |
| 185 | + out += fmt.Sprintf("%s\t}\n", indent) |
| 186 | + out += fmt.Sprintf("%s}\n", indent) |
| 187 | + return out |
| 188 | +} |
0 commit comments