-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds params to customize TektonAddon component on OpenShift
- This adds params in TektonConfig which would be copied to Tekton Addon CR to customize installation of different components of Addons. - If the params are not passed in TektonConfig CR, controller will add them with default values. - User can change values in TektonConfig to change the component installation of Addon. - If Addon is created through TektonConfig CR, and if later values are changed in Addon CR, it will get overwritten by TektonConfig CR values. Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
- Loading branch information
1 parent
5bf5620
commit e657bbb
Showing
22 changed files
with
522 additions
and
18 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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,89 @@ | ||
/* | ||
Copyright 2021 The Tekton 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 common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
// ValidateParamsAndSetDefault validates the params and their values with the component params passed | ||
// If the params from spec is missing some params then this will add them with default values | ||
// If there is need to check any special conditions, a func can be passed to validate the same params | ||
// which will be called before setting the default values | ||
func ValidateParamsAndSetDefault(ctx context.Context, params *[]v1alpha1.Param, | ||
componentParams map[string]v1alpha1.ParamValue, validateCond func(params *[]v1alpha1.Param) error) (bool, error) { | ||
|
||
updated := false | ||
logger := logging.FromContext(ctx) | ||
|
||
// Validate all params passed are valid and have valid values | ||
for _, p := range *params { | ||
pv, ok := componentParams[p.Name] | ||
if !ok { | ||
logger.Error("invalid param: %s") | ||
return updated, fmt.Errorf("invalid param : %s", p.Name) | ||
} | ||
if !isParamValueValid(p.Value, pv.Possible) { | ||
msg := fmt.Sprintf("invalid value (%s) for param: %s", p.Value, p.Name) | ||
logger.Error(msg) | ||
return updated, fmt.Errorf(msg) | ||
} | ||
} | ||
|
||
err := validateCond(params) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// Parse params and convert in a map | ||
specParams := ParseParams(*params) | ||
|
||
// If a param is not passed, add the param with default value | ||
for d := range componentParams { | ||
_, ok := specParams[d] | ||
if !ok { | ||
*params = append(*params, | ||
v1alpha1.Param{ | ||
Name: d, | ||
Value: componentParams[d].Default, | ||
}) | ||
updated = true | ||
} | ||
} | ||
return updated, nil | ||
} | ||
|
||
// ParseParams returns the params passed in a map | ||
func ParseParams(params []v1alpha1.Param) map[string]string { | ||
paramsMap := map[string]string{} | ||
for _, p := range params { | ||
paramsMap[p.Name] = p.Value | ||
} | ||
return paramsMap | ||
} | ||
|
||
func isParamValueValid(value string, possible []string) bool { | ||
for _, v := range possible { | ||
if v == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,83 @@ | ||
/* | ||
Copyright 2021 The Tekton 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 common | ||
|
||
import ( | ||
"context" | ||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
"gotest.tools/assert" | ||
"testing" | ||
) | ||
|
||
func nullFunc() func(params *[]v1alpha1.Param) error { | ||
return func(params *[]v1alpha1.Param) error { | ||
return nil | ||
} | ||
} | ||
|
||
func TestValidateParamsAndSetDefault(t *testing.T) { | ||
|
||
componentParams := map[string]v1alpha1.ParamValue{ | ||
"valid": v1alpha1.ParamValue{ | ||
Default: "param", | ||
Possible: []string{"param", "param1"}, | ||
}, | ||
"newParam": v1alpha1.ParamValue{ | ||
Default: "newValue", | ||
Possible: []string{"newValue", "newValue1"}, | ||
}, | ||
} | ||
|
||
t.Run("Invalid Param", func(t *testing.T) { | ||
params := []v1alpha1.Param{ | ||
{Name: "foo", Value: "bar"}, | ||
} | ||
_, err := ValidateParamsAndSetDefault(context.TODO(), ¶ms, componentParams, nullFunc()) | ||
assert.Error(t, err, "invalid param : foo") | ||
}) | ||
|
||
t.Run("Invalid Param Value", func(t *testing.T) { | ||
params := []v1alpha1.Param{ | ||
{Name: "valid", Value: "bar"}, | ||
} | ||
_, err := ValidateParamsAndSetDefault(context.TODO(), ¶ms, componentParams, nullFunc()) | ||
assert.Error(t, err, "invalid value (bar) for param: valid") | ||
}) | ||
|
||
t.Run("All params are defined so no error", func(t *testing.T) { | ||
params := []v1alpha1.Param{ | ||
{Name: "valid", Value: "param1"}, | ||
{Name: "newParam", Value: "newValue"}, | ||
} | ||
updated, err := ValidateParamsAndSetDefault(context.TODO(), ¶ms, componentParams, nullFunc()) | ||
assert.NilError(t, err) | ||
assert.Equal(t, updated, false) | ||
}) | ||
|
||
t.Run("Some params are missing", func(t *testing.T) { | ||
params := []v1alpha1.Param{ | ||
{Name: "valid", Value: "param1"}, | ||
} | ||
updated, err := ValidateParamsAndSetDefault(context.TODO(), ¶ms, componentParams, nullFunc()) | ||
assert.NilError(t, err) | ||
// since some params are added to spec, updated should be true | ||
assert.Equal(t, updated, true) | ||
// missing params will be added with default value | ||
assert.Equal(t, params[1].Name, "newParam") | ||
assert.Equal(t, params[1].Value, "newValue") | ||
}) | ||
} |
Oops, something went wrong.