Skip to content

Commit

Permalink
fix:move getFeatureFlagsBaseOnAPIFlag from custom_task_test to anothe…
Browse files Browse the repository at this point in the history
…r file #7269
  • Loading branch information
Dhruval7878 committed Oct 30, 2023
1 parent 638baf2 commit 912705b
Show file tree
Hide file tree
Showing 2 changed files with 62 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,19 +20,15 @@ limitations under the License.
package test

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

"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,62 +721,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",
})
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
}
62 changes: 62 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,62 @@ 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",
})
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 912705b

Please sign in to comment.