Skip to content

Cleanup namespaces #4985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions scripts/clean-old-namespace-from-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env sh
# this scripts logins as kubeadmin and removes the namespaces that are more than oneday old.
# this script is used in a kubernetes cronjob for PSI/IBM cloud openshift cluster.

# login as kubeadmin
if [[ $CLUSTER_TYPE == "PSI" ]]; then
 #PSI cluster login
 oc login -u kubeadmin -p ${OCP4X_KUBEADMIN_PASSWORD} --insecure-skip-tls-verify ${OCP4X_API_URL}
else
 # Login to IBM Cloud using service account API Key
 ibmcloud login --apikey $IBMC_OCP47_APIKEY -a cloud.ibm.com -r eu-de -g "Developer CI and QE"
 # Login to cluster in IBM Cloud using cluster API key
 oc login --token=$IBMC_OCLOGIN_APIKEY --server=$IBMC_OCP47_SERVER
fi

# PROJECT_AND_TIME var will contain namespace and date with time seperated with `|`
# PROJECT_AND_TIME doesn't contain openshift/ibm/kube namespace
# eg. cmd-push-test157kgb|2021-08-17T12:40:20Z
PROJECT_AND_TIME=$(kubectl get projects -o jsonpath='{range .items[*]}{.metadata.name}{"|"}{.metadata.creationTimestamp} {"\n"}{end}' | grep -v '^openshift\|^kube\|^default\|^ibm\|^calico\|^tigera\|^odo-operator-test')

for PROJECT in ${PROJECT_AND_TIME}; do
IFS='|' read -r PRJ TIME <<<"$PROJECT" # seperate the Namespace and time of creation using IFS(Input Field Seperators) value `|`
echo "INFO: Project="${PRJ}, "Date=" ${TIME}

dtSec=$(date --date "${TIME}" +'%s') # convert time in sec for namespace age
taSec=$(date --date "1 days ago" +'%s') # convert time allowed for the namespace to be in the cluster

if [ $dtSec -lt $taSec ]; then
echo too old project : ${TIME}
# delete namespace
oc delete project ${PRJ}
echo ---------
fi
done
1 change: 1 addition & 0 deletions tests/e2escenarios/e2e_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var _ = Describe("odo supported images e2e tests", func() {
// initialize oc runner
oc = helper.NewOcRunner("oc")
commonVar = helper.CommonBeforeEach()
oc.AddSecret(commonVar)
})

// Clean up after the test
Expand Down
1 change: 1 addition & 0 deletions tests/e2escenarios/e2e_java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var _ = Describe("odo java e2e tests", func() {
// initialize oc runner
oc = helper.NewOcRunner("oc")
commonVar = helper.CommonBeforeEach()
oc.AddSecret(commonVar)
})

// Clean up after the test
Expand Down
1 change: 1 addition & 0 deletions tests/e2escenarios/e2e_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var _ = Describe("odo source e2e tests", func() {
// initialize oc runner
oc = helper.NewOcRunner("oc")
commonVar = helper.CommonBeforeEach()
oc.AddSecret(commonVar)
})

// Clean up after the test
Expand Down
63 changes: 61 additions & 2 deletions tests/helper/helper_oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package helper
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -628,8 +630,7 @@ func (oc OcRunner) CreateRandNamespaceProjectOfLength(i int) string {

func (oc OcRunner) createRandNamespaceProject(projectName string) string {
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
session := Cmd("odo", "project", "create", projectName, "-w", "-v4").ShouldPass().Out()
Expect(session).To(ContainSubstring("New project created"))
session := Cmd("oc", "new-project", projectName).ShouldPass().Out()
Expect(session).To(ContainSubstring(projectName))
return projectName
}
Expand Down Expand Up @@ -766,3 +767,61 @@ func (oc OcRunner) CreateSecret(secretName, secretPass, project string) {
func (oc OcRunner) GetSecrets(project string) string {
return GetSecrets(oc.path, project)
}

// AddSecret adds pull-secret to the namespace, for e2e-test
func (oc OcRunner) AddSecret(comvar CommonVar) {

clusterType := os.Getenv("CLUSTER_TYPE")
if clusterType == "PSI" || clusterType == "IBM" {

token := oc.doAsAdmin(clusterType)

yaml := Cmd(oc.path, "get", "secret", "pull-secret", "-n", "openshift-config", "-o", "yaml").ShouldPass().Out()
newYaml := strings.Replace(yaml, "openshift-config", comvar.Project, -1)
filename := fmt.Sprint(RandString(4), ".yaml")
newYamlinByte := []byte(newYaml)
err := ioutil.WriteFile(filename, newYamlinByte, 0600)
if err != nil {
fmt.Println(err)
}
Cmd(oc.path, "apply", "-f", filename).ShouldPass()
os.Remove(filename)
oc.doAsDeveloper(token, clusterType)
}

}

// doAsAdmin logins as admin to perform some task that requires admin privileges
func (oc OcRunner) doAsAdmin(clusterType string) string {
//save token for developer
token := oc.GetToken()
if clusterType == "PSI" || clusterType == "IBM" {

adminToken := os.Getenv("IBMC_OCLOGIN_APIKEY")
if adminToken != "" {
ibmcloudAdminToken := os.Getenv("IBMC_ADMIN_LOGIN_APIKEY")
cluster := os.Getenv("IBMC_OCP47_SERVER")
//login ibmcloud
Cmd("ibmcloud", "login", "--apikey", ibmcloudAdminToken, "-r", "eu-de", "-g", "Developer-CI-and-QE")
//login as admin in cluster
Cmd(oc.path, "login", "--token=", adminToken, "--server=", cluster)
} else {
pass := os.Getenv("OCP4X_KUBEADMIN_PASSWORD")
cluster := os.Getenv("OCP4X_API_URL")
//login as kubeadmin
Cmd(oc.path, "login", "-u", "kubeadmin", "-p", pass, cluster).ShouldPass()
}
}
return token
}

// doAsDeveloper logins as developer to perform some task
func (oc OcRunner) doAsDeveloper(token, clusterType string) {

if clusterType == "IBM" {
ibmcloudDeveloperToken := os.Getenv("IBMC_DEVELOPER_LOGIN_APIKEY")
Cmd("ibmcloud", "login", "--apikey", ibmcloudDeveloperToken, "-r", "eu-de", "-g", "Developer-CI-and-QE")
//login as developer using token
}
oc.LoginUsingToken(token)
}