Skip to content

Commit

Permalink
Change how test namespace name for e2e tests are generated to avoid c…
Browse files Browse the repository at this point in the history
…onflicts when names are too long (okteto#4678)

* Change how test namespace name for e2e tests are generated to avoid conflicts when names are too long

Signed-off-by: Nacho Fuertes <[email protected]>

* Take into account prefix when building namespace name

Signed-off-by: Nacho Fuertes <[email protected]>

* Changed how namespace names are generated. Using hash

Signed-off-by: Nacho Fuertes <[email protected]>

* Removed unused function

Signed-off-by: Nacho Fuertes <[email protected]>

* Install latest CLI on action tests jobs

Signed-off-by: Nacho Fuertes <[email protected]>

* Removed another unused function

Signed-off-by: Nacho Fuertes <[email protected]>

* Removed unused function

Signed-off-by: Nacho Fuertes <[email protected]>

---------

Signed-off-by: Nacho Fuertes <[email protected]>
  • Loading branch information
ifbyol authored Mar 3, 2025
1 parent 91b85f3 commit ca5ed5a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ aliases:
- &release-regex /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
- &release-branch-regex /^release-\d+\.\d+$/
- &docker-login echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
- &install-cli curl https://get.okteto.com -sSfL | sh

parameters:
# The following parameters are filled by GH Actions to run CircleCI jobs
Expand Down Expand Up @@ -155,6 +156,8 @@ jobs:
- v4-pkg-cache-{{ checksum "go.sum" }}
- attach_workspace:
at: ./artifacts
# Install the latest CLI public available, as the actions tests uses the installed version to checkout the action branch to test
- run: *install-cli
- run:
name: Run actions integration tests
command: |
Expand Down
33 changes: 0 additions & 33 deletions integration/k8s-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,12 @@ import (
"time"

"github.com/okteto/okteto/integration/commands"
"github.com/okteto/okteto/pkg/config"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// K8sClient returns a kubernetes client for current KUBECONFIG
func K8sClient() (*kubernetes.Clientset, *rest.Config, error) {
clientConfig := getClientConfig(config.GetKubeconfigPath(), "")

config, err := clientConfig.ClientConfig()
if err != nil {
return nil, nil, err
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, err
}
return client, config, nil
}

func getClientConfig(kubeconfigPaths []string, kubeContext string) clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
loadingRules.Precedence = kubeconfigPaths
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
&clientcmd.ConfigOverrides{
CurrentContext: kubeContext,
ClusterInfo: clientcmdapi.Cluster{Server: ""},
},
)
}

func GetPodsBySelector(ctx context.Context, ns, selector string, client kubernetes.Interface) (*corev1.PodList, error) {
return client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector})
}
Expand Down
26 changes: 6 additions & 20 deletions integration/okteto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package integration

import (
"crypto/sha256"
"fmt"
"io"
"log"
Expand All @@ -26,8 +27,6 @@ import (
"testing"
"time"

"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/k8s/kubeconfig"
"github.com/okteto/okteto/pkg/model"
"github.com/okteto/okteto/pkg/okteto"
)
Expand Down Expand Up @@ -79,16 +78,6 @@ func RunOktetoVersion(oktetoPath string) (string, error) {
return string(o), nil
}

func reduceName(s string) string {
return strings.Map(func(r rune) rune {
switch r {
case 'a', 'e', 'i', 'o', 'u', '_':
return -1
}
return r
}, s)
}

// GetTestNamespace returns the name for a namespace
func GetTestNamespace(name string) string {
runtimeOS := runtime.GOOS
Expand All @@ -97,16 +86,13 @@ func GetTestNamespace(name string) string {
} else {
runtimeOS = runtimeOS[:3]
}
name = reduceName(strings.ToLower(name))
// To avoid namespace name conflicts, we hash the test name, and add a timestamp and the OS as prefix
sha := fmt.Sprintf("%x", sha256.Sum256([]byte(name)))
finalName := fmt.Sprintf("%d-%s-%s", time.Now().Unix(), runtimeOS, sha[:8])
if prefix := os.Getenv("OKTETO_NAMESPACE_PREFIX"); prefix != "" {
name = fmt.Sprintf("%s-%s", prefix, name)
finalName = fmt.Sprintf("%s-%s", prefix, finalName)
}
return strings.ToLower(fmt.Sprintf("%s-%s-%d", name, runtimeOS, time.Now().Unix()))
}

// GetCurrentNamespace returns the current namespace of the kubeconfig path
func GetCurrentNamespace() string {
return kubeconfig.CurrentNamespace(config.GetKubeconfigPath())
return finalName
}

// GetContentFromURL returns the content of the url
Expand Down

0 comments on commit ca5ed5a

Please sign in to comment.