Skip to content

Commit

Permalink
fix: move getFeatureFlagsBaseOnAPIFlag from custom_task_test to anoth…
Browse files Browse the repository at this point in the history
…er file
  • Loading branch information
Dhruval7878 committed Nov 20, 2023
1 parent 23581c5 commit 17b8153
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 63 deletions.
63 changes: 0 additions & 63 deletions test/custom_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ limitations under the License.
package test

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"sync"
Expand All @@ -32,7 +30,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
Expand Down Expand Up @@ -725,63 +722,3 @@ func resetConfigMap(ctx context.Context, t *testing.T, c *clients, namespace, co
t.Log(err)
}
}

func getFeatureFlagsBaseOnAPIFlag(t *testing.T) *config.FeatureFlags {
t.Helper()
alphaFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "alpha",
"results-from": "sidecar-logs",
"enable-tekton-oci-bundles": "true",
"enable-step-actions": "true",
"enable-cel-in-whenexpression": "true",
"enable-param-enum": "true",
})
if err != nil {
t.Fatalf("error creating alpha feature flags configmap: %v", err)
}
betaFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "beta",
})
if err != nil {
t.Fatalf("error creating beta feature flags configmap: %v", err)
}
stableFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "stable",
})
if err != nil {
t.Fatalf("error creating stable feature flags configmap: %v", err)
}
enabledFeatureGate, err := getAPIFeatureGate()
if err != nil {
t.Fatalf("error reading enabled feature gate: %v", err)
}
switch enabledFeatureGate {
case "alpha":
return alphaFeatureFlags
case "beta":
return betaFeatureFlags
default:
return stableFeatureFlags
}
}

// getAPIFeatureGate queries the tekton pipelines namespace for the
// current value of the "enable-api-fields" feature gate.
func getAPIFeatureGate() (string, error) {
ns := os.Getenv("SYSTEM_NAMESPACE")
if ns == "" {
ns = "tekton-pipelines"
}

cmd := exec.Command("kubectl", "get", "configmap", "feature-flags", "-n", ns, "-o", `jsonpath="{.data['enable-api-fields']}"`)
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("error getting feature-flags configmap: %w", err)
}
output = bytes.TrimSpace(output)
output = bytes.Trim(output, "\"")
if len(output) == 0 {
output = []byte("stable")
}
return string(output), nil
}
63 changes: 63 additions & 0 deletions test/featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package test

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"testing"

Expand Down Expand Up @@ -104,3 +107,63 @@ func requireAllGates(gates map[string]string) func(context.Context, *testing.T,
}
}
}

func getFeatureFlagsBaseOnAPIFlag(t *testing.T) *config.FeatureFlags {
t.Helper()
alphaFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "alpha",
"results-from": "sidecar-logs",
"enable-tekton-oci-bundles": "true",
"enable-step-actions": "true",
"enable-cel-in-whenexpression": "true",
"enable-param-enum": "true",
})
if err != nil {
t.Fatalf("error creating alpha feature flags configmap: %v", err)
}
betaFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "beta",
})
if err != nil {
t.Fatalf("error creating beta feature flags configmap: %v", err)
}
stableFeatureFlags, err := config.NewFeatureFlagsFromMap(map[string]string{
"enable-api-fields": "stable",
})
if err != nil {
t.Fatalf("error creating stable feature flags configmap: %v", err)
}
enabledFeatureGate, err := getAPIFeatureGate()
if err != nil {
t.Fatalf("error reading enabled feature gate: %v", err)
}
switch enabledFeatureGate {
case "alpha":
return alphaFeatureFlags
case "beta":
return betaFeatureFlags
default:
return stableFeatureFlags
}
}

// getAPIFeatureGate queries the tekton pipelines namespace for the
// current value of the "enable-api-fields" feature gate.
func getAPIFeatureGate() (string, error) {
ns := os.Getenv("SYSTEM_NAMESPACE")
if ns == "" {
ns = "tekton-pipelines"
}

cmd := exec.Command("kubectl", "get", "configmap", "feature-flags", "-n", ns, "-o", `jsonpath="{.data['enable-api-fields']}"`)
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("error getting feature-flags configmap: %w", err)
}
output = bytes.TrimSpace(output)
output = bytes.Trim(output, "\"")
if len(output) == 0 {
output = []byte("stable")
}
return string(output), nil
}

0 comments on commit 17b8153

Please sign in to comment.