Skip to content

Commit d0f4d94

Browse files
authored
feat: opsdefinition env support optional setting (#9438)
1 parent e1bed8e commit d0f4d94

6 files changed

Lines changed: 31 additions & 0 deletions

File tree

apis/operations/v1alpha1/opsdefinition_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ type OpsEnvVar struct {
134134
//
135135
// +kubebuilder:validation:Required
136136
ValueFrom *OpsVarSource `json:"valueFrom"`
137+
138+
// Specify whether the ENV must be defined.
139+
//
140+
// +optional
141+
Optional *bool `json:"optional,omitempty"`
137142
}
138143

139144
// +kubebuilder:validation:XValidation:rule="has(self.envRef) || has(self.fieldPath)", message="either fieldPath and envRef."

apis/operations/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/operations.kubeblocks.io_opsdefinitions.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7973,6 +7973,9 @@ spec:
79737973
Specifies the name of the environment variable to be injected into Pods executing OpsActions.
79747974
It must conform to the C_IDENTIFIER format, which includes only alphanumeric characters and underscores, and cannot begin with a digit.
79757975
type: string
7976+
optional:
7977+
description: Specify whether the ENV must be defined.
7978+
type: boolean
79767979
valueFrom:
79777980
description: Specifies the source of the environment variable's
79787981
value.

deploy/helm/crds/operations.kubeblocks.io_opsdefinitions.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7973,6 +7973,9 @@ spec:
79737973
Specifies the name of the environment variable to be injected into Pods executing OpsActions.
79747974
It must conform to the C_IDENTIFIER format, which includes only alphanumeric characters and underscores, and cannot begin with a digit.
79757975
type: string
7976+
optional:
7977+
description: Specify whether the ENV must be defined.
7978+
type: boolean
79767979
valueFrom:
79777980
description: Specifies the source of the environment variable's
79787981
value.

docs/developer_docs/api-reference/operations.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,18 @@ OpsVarSource
18811881
<p>Specifies the source of the environment variable&rsquo;s value.</p>
18821882
</td>
18831883
</tr>
1884+
<tr>
1885+
<td>
1886+
<code>optional</code><br/>
1887+
<em>
1888+
bool
1889+
</em>
1890+
</td>
1891+
<td>
1892+
<em>(Optional)</em>
1893+
<p>Specify whether the ENV must be defined.</p>
1894+
</td>
1895+
</tr>
18841896
</tbody>
18851897
</table>
18861898
<h3 id="operations.kubeblocks.io/v1alpha1.OpsExecAction">OpsExecAction

pkg/operations/custom/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ func buildEnvVars(reqCtx intctrlutil.RequestCtx,
176176
envVar, err = buildVarWithFieldPath(targetPod, vars[i].ValueFrom.FieldRef.FieldPath)
177177
}
178178
if envVar == nil {
179+
if vars[i].Optional != nil && *vars[i].Optional {
180+
continue
181+
}
179182
return nil, intctrlutil.NewFatalError(fmt.Sprintf(`can not find the env "%s" in the container "%s"`, envVarRef.EnvName, envVarRef.TargetContainerName))
180183
}
181184
envVar.Name = vars[i].Name

0 commit comments

Comments
 (0)