Skip to content
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

Conformance: update objects in MustApplyObjectsWithCleanup #2380

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Conformance: update objects in MustApplyObjectsWithCleanup
Signed-off-by: sadath-12 <sadathsadu2002@gmail.com>
  • Loading branch information
sadath-12 committed Sep 7, 2023
commit 35c33e0aa0c03d512cba3711dde3a6177168fbd2
3 changes: 2 additions & 1 deletion conformance/base/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ metadata:
spec:
selector:
app: infra-backend-v3
ports:
ports:
- protocol: TCP
port: 8080
targetPort: 3000
Expand Down Expand Up @@ -496,3 +496,4 @@ spec:
resources:
requests:
cpu: 10m

25 changes: 25 additions & 0 deletions conformance/mesh/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,28 @@ spec:
- name: echo
image: gcr.io/k8s-staging-gateway-api/echo-server:v20230505-v0.7.0-rc1-10-g497e67da
imagePullPolicy: IfNotPresent

---

apiVersion: apps/v1
kind: Deployment
metadata:
name: echo-v1
namespace: gateway-conformance-mesh-consumer
labels:
app: echo-v2
spec:
selector:
matchLabels:
app: echo-v2
version: v1
template:
metadata:
labels:
app: echo-v2
version: v1
spec:
containers:
- name: echo
image: gcr.io/k8s-staging-gateway-api/echo-server:v20230505-v0.7.0-rc1-10-g497e67da
imagePullPolicy: IfNotPresent
65 changes: 39 additions & 26 deletions conformance/utils/kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,46 @@ func (a Applier) prepareResources(t *testing.T, decoder *yaml.YAMLOrJSONDecoder)
}

func (a Applier) MustApplyObjectsWithCleanup(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, resources []client.Object, cleanup bool) {
for _, resource := range resources {
resource := resource

ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.CreateTimeout)
defer cancel()

t.Logf("Creating %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)

err := c.Create(ctx, resource)
if err != nil {
if !apierrors.IsAlreadyExists(err) {
require.NoError(t, err, "error creating resource")
}
}

if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
err = c.Delete(ctx, resource)
require.NoErrorf(t, err, "error deleting resource")
})
}
}
fmt.Print("heyyy")
for _, resource := range resources {
resource := resource

ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.CreateTimeout)
defer cancel()

t.Logf("Creating/Updating %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)

// Check if the resource exists
existingResource := resource.DeepCopyObject().(client.Object)
err := c.Get(ctx, types.NamespacedName{Name: resource.GetName(), Namespace: resource.GetNamespace()}, existingResource)
if err != nil {
if !apierrors.IsNotFound(err) {
t.Logf("error checking resource: %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
require.NoError(t, err, "error checking resource")
}

// Resource doesn't exist, create it
err = c.Create(ctx, resource)
require.NoError(t, err, "error creating resource")
} else {
// Resource exists, update it
err = c.Update(ctx, resource)
require.NoError(t, err, "error updating resource")
}

if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
err = c.Delete(ctx, resource)
require.NoErrorf(t, err, "error deleting resource")
})
}
}
}


// MustApplyWithCleanup creates or updates Kubernetes resources defined with the
// provided YAML file and registers a cleanup function for resources it created.
// Note that this does not remove resources that already existed in the cluster.
Expand All @@ -172,7 +185,7 @@ func (a Applier) MustApplyWithCleanup(t *testing.T, c client.Client, timeoutConf
require.NoError(t, err)

decoder := yaml.NewYAMLOrJSONDecoder(data, 4096)

resources, err := a.prepareResources(t, decoder)
if err != nil {
t.Logf("manifest: %s", data.String())
Expand Down
2 changes: 1 addition & 1 deletion conformance/utils/kubernetes/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func MustCreateSelfSignedCertSecret(t *testing.T, namespace, secretName string,
data := map[string][]byte{
corev1.TLSCertKey: serverCert.Bytes(),
corev1.TLSPrivateKeyKey: serverKey.Bytes(),
}
}

newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down
7 changes: 4 additions & 3 deletions conformance/utils/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func New(s Options) *ConformanceTestSuite {

// Setup ensures the base resources required for conformance tests are installed
// in the cluster. It also ensures that all relevant resources are ready.
func (suite *ConformanceTestSuite) Setup(t *testing.T) {
func (suite *ConformanceTestSuite) Setup(t *testing.T) {
suite.Applier.FS = suite.FS

if suite.SupportedFeatures.Has(SupportGateway) {
t.Logf("Test Setup: Ensuring GatewayClass has been accepted")
suite.ControllerName = kubernetes.GWCMustHaveAcceptedConditionTrue(t, suite.Client, suite.TimeoutConfig, suite.GatewayClassName)
suite.ControllerName = kubernetes.GWCMustHaveAcceptedConditionTrue(t, suite.Client, suite.TimeoutConfig, "nginx")

suite.Applier.GatewayClass = suite.GatewayClassName
suite.Applier.ControllerName = suite.ControllerName
Expand All @@ -156,7 +156,8 @@ func (suite *ConformanceTestSuite) Setup(t *testing.T) {

t.Logf("Test Setup: Applying programmatic resources")
secret := kubernetes.MustCreateSelfSignedCertSecret(t, "gateway-conformance-web-backend", "certificate", []string{"*"})
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{secret}, suite.Cleanup)
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{secret}, suite.Cleanup)

secret = kubernetes.MustCreateSelfSignedCertSecret(t, "gateway-conformance-infra", "tls-validity-checks-certificate", []string{"*", "*.org"})
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{secret}, suite.Cleanup)
secret = kubernetes.MustCreateSelfSignedCertSecret(t, "gateway-conformance-infra", "tls-passthrough-checks-certificate", []string{"abc.example.com"})
Expand Down