-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create utils functions for e2e tests.
Create util functions for setup/create gcloud resources. Add function to check ingress IP and validate its status.
- Loading branch information
Showing
8 changed files
with
622 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 | ||
https://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 utils | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
func SetupCluster(clusterName, zone, networkName string, numOfNodes int) error { | ||
klog.Infof("Setting up cluster %s in zone %s in network %s with %d node(s).", clusterName, zone, networkName, numOfNodes) | ||
params := []string{ | ||
"container", | ||
"clusters", | ||
"describe", | ||
clusterName, | ||
"--zone", zone, | ||
} | ||
out, err := exec.Command("gcloud", params...).CombinedOutput() | ||
if err != nil { | ||
klog.Infof("Cluster %s does not exist, creating.", clusterName) | ||
return createCluster(clusterName, zone, networkName, numOfNodes) | ||
} | ||
|
||
pattern := "currentNodeCount: " | ||
val, err := checkField([]byte(pattern), out) | ||
if err != nil { | ||
klog.Infof("Cannot get the node count within the cluster description. Delete and recreate cluster %s in %s.", clusterName, zone) | ||
return deleteAndCreateCluster(clusterName, zone, networkName, numOfNodes) | ||
} | ||
gotNumOfNodes, err := strconv.Atoi(val) | ||
if err != nil || gotNumOfNodes != numOfNodes { | ||
klog.Infof("Got cluster with %d nodes, expect %d. Delete and recreate cluster %s in %s.", gotNumOfNodes, numOfNodes, clusterName, zone) | ||
return deleteAndCreateCluster(clusterName, zone, networkName, numOfNodes) | ||
} | ||
klog.Infof("Use existing cluster %s in zone %s with %d nodes.", clusterName, zone, numOfNodes) | ||
return nil | ||
} | ||
|
||
func createCluster(clusterName, zone, networkName string, numOfNodes int) error { | ||
klog.Infof("Creating cluster %s in %s in network %s with %d node(s).", clusterName, zone, networkName, numOfNodes) | ||
params := []string{ | ||
"container", | ||
"clusters", | ||
"create", | ||
clusterName, | ||
"--network", networkName, | ||
"--subnetwork", networkName, | ||
"--zone", zone, | ||
"--num-nodes", strconv.Itoa(numOfNodes), | ||
} | ||
if out, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed creating cluster: %s, err: %v", out, err) | ||
} | ||
return nil | ||
} | ||
|
||
func DeleteCluster(clusterName, zone string) error { | ||
klog.Infof("Deleting cluster %s in %s", clusterName, zone) | ||
params := []string{ | ||
"container", | ||
"clusters", | ||
"delete", | ||
clusterName, | ||
"--zone", | ||
zone, | ||
"--quiet", | ||
} | ||
if out, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed deleting cluster: %s, err: %v", out, err) | ||
} | ||
return nil | ||
} | ||
|
||
func deleteAndCreateCluster(clusterName, zone, networkName string, numOfNodes int) error { | ||
if err := DeleteCluster(clusterName, zone); err != nil { | ||
return fmt.Errorf("failed delete and create cluster: %s, err: %v", clusterName, err) | ||
} | ||
if err := createCluster(clusterName, zone, networkName, numOfNodes); err != nil { | ||
return fmt.Errorf("failed delete and create cluster: %s, err: %v", clusterName, err) | ||
} | ||
return nil | ||
} | ||
|
||
func GetCredential(zone, clusterName string) error { | ||
klog.Infof("Getting credential for cluster %s in zone %s.", clusterName, zone) | ||
params := []string{ | ||
"container", | ||
"clusters", | ||
"get-credentials", | ||
clusterName, | ||
"--zone", | ||
zone, | ||
} | ||
if out, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed setting kubeconfig: %s, err: %v", out, err) | ||
} | ||
return nil | ||
} |
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,92 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 | ||
https://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 utils | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
func SetupAllowProxyConnectionFirewall(firewallName, networkName string) error { | ||
klog.Infof("Setting up allow-proxy-connection firewall %s in network %s.", firewallName, networkName) | ||
params := []string{ | ||
"compute", | ||
"firewall-rules", | ||
"describe", | ||
firewallName, | ||
} | ||
if _, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
klog.Infof("allow-proxy-connection firewall %s does not exist, creating.", firewallName) | ||
return createAllowProxyConnectionFirewall(firewallName, networkName) | ||
} | ||
klog.Infof("Use existing allow-proxy-connection firewall %s.", firewallName) | ||
return nil | ||
} | ||
|
||
func createAllowProxyConnectionFirewall(firewallName, networkName string) error { | ||
klog.Infof("Creating allow-proxy-connection firewall %s in network %s.", firewallName, networkName) | ||
params := []string{ | ||
"compute", | ||
"firewall-rules", | ||
"create", | ||
firewallName, | ||
"--allow", "TCP:8080", | ||
"--source-ranges", "10.129.0.0/23", | ||
"--network", networkName, | ||
} | ||
if out, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed creating allow-proxy-connection firewall %s: %s, err: %v", firewallName, out, err) | ||
} | ||
return nil | ||
} | ||
|
||
func SetupAllowSSHFirewall(firewallName, networkName string) error { | ||
klog.Infof("Setting up allow-ssh firewall %s in network %s.", firewallName, networkName) | ||
params := []string{ | ||
"compute", | ||
"firewall-rules", | ||
"describe", | ||
firewallName, | ||
} | ||
if _, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
klog.Infof("allow-ssh firewall %s does not exist, creating.", firewallName) | ||
return createAllowSSHFirewall(firewallName, networkName) | ||
} | ||
klog.Infof("Use existing allow-ssh firewall %s.", firewallName) | ||
return nil | ||
} | ||
|
||
func createAllowSSHFirewall(firewallName, networkName string) error { | ||
klog.Infof("Creating allow-ssh firewall %s in network %s.", firewallName, networkName) | ||
params := []string{ | ||
"compute", | ||
"firewall-rules", | ||
"create", | ||
firewallName, | ||
"--network", networkName, | ||
"--action", "allow", | ||
"--direction", "ingress", | ||
"--target-tags", "allow-ssh", | ||
"--rules", "tcp:22", | ||
} | ||
if out, err := exec.Command("gcloud", params...).CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed creating allow-ssh firewall %s: %s, err: %v", firewallName, out, err) | ||
} | ||
return nil | ||
} |
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,56 @@ | ||
/* | ||
Copyright 2023 The Kubernetes 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 | ||
https://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 utils | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
networkingv1 "k8s.io/api/networking/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/klog/v2" | ||
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
ingressPollInterval = 30 * time.Second | ||
ingressPollTimeout = 45 * time.Minute | ||
) | ||
|
||
func fetchIngress(ctx context.Context, cli ctrlClient.Client, key ctrlClient.ObjectKey) (*networkingv1.Ingress, error) { | ||
obj := &networkingv1.Ingress{} | ||
return obj, cli.Get(ctx, key, obj) | ||
} | ||
|
||
func WaitForIngress(ctx context.Context, cli ctrlClient.Client, ingressObjectKey ctrlClient.ObjectKey) (string, error) { | ||
var ingressIP string | ||
err := wait.PollUntilContextTimeout(ctx, ingressPollInterval, ingressPollTimeout, true, func(ctx context.Context) (done bool, err error) { | ||
ing, err := fetchIngress(ctx, cli, ingressObjectKey) | ||
if err != nil { | ||
klog.Infof("fetchIngress(%s)=%v", ingressObjectKey, err) | ||
return false, nil | ||
} | ||
ingressIPs := ing.Status.LoadBalancer.Ingress | ||
if len(ingressIPs) < 1 || ingressIPs[0].IP == "" { | ||
klog.Infof("Invalid ingress IP %s when fetchIngress(%s)", ingressIPs, ingressObjectKey) | ||
return false, nil | ||
} | ||
ingressIP = ingressIPs[0].IP | ||
return true, nil | ||
}) | ||
return ingressIP, err | ||
} |
Oops, something went wrong.