Skip to content

Commit

Permalink
Merge pull request #415 from heylongdacoder/third-party-ko
Browse files Browse the repository at this point in the history
third_party/ko: Add ko as third party tools
  • Loading branch information
k8s-ci-robot authored Jun 25, 2024
2 parents 0b86d86 + 1e04273 commit c1ac086
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 2 deletions.
3 changes: 2 additions & 1 deletion examples/third_party_integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ This section of the repository contains the example of how the third party tooli
`e2e-framework`

1. [Helm](./helm)
2. [Flux](./flux)
2. [Flux](./flux)
3. [Ko](./ko)
102 changes: 102 additions & 0 deletions examples/third_party_integration/ko/ko_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2024 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
http://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 ko

import (
"context"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
"sigs.k8s.io/e2e-framework/third_party/ko"
)

var packagePath = "./" + filepath.Join(curDir, "testdata", "example_goapp")

func TestBuildLocalKind(t *testing.T) {
feature := features.New("ko build with local kind cluster").
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
var err error

// Apple Silicon with Podman as kind provider need to build with linux/arm64
platform := fmt.Sprintf("linux/%s", runtime.GOARCH)

manager := ko.New()
err = manager.Install("latest")
if err != nil {
t.Fatalf("failed to install ko: %v", err)
}

ctx, err = manager.BuildLocal(ctx, packagePath, ko.WithLocalKindName(kindClusterName), ko.WithPlatforms(platform))
if err != nil {
t.Fatalf("failed to build with local kind: %v", err)
}

return ctx
}).
Assess("Deployment is running successfully", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
manager := ko.New()
goappImage, err := manager.GetLocalImage(ctx, packagePath)
if err != nil {
t.Fatalf("failed to get previous built image: %v", err)
}

deployment := newDeployment(config.Namespace(), goappImage, 1)
client, err := config.NewClient()
if err != nil {
t.Fatalf("failed to init k8s client: %v", err)
}
if err = client.Resources().Create(ctx, deployment); err != nil {
t.Fatalf("failed to create deployment: %v", err)
}
err = wait.For(conditions.New(client.Resources()).DeploymentConditionMatch(deployment, appsv1.DeploymentAvailable, corev1.ConditionTrue), wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatalf("failed to wait for deployment to be ready: %v", err)
}

return ctx
}).Feature()

_ = testEnv.Test(t, feature)
}

func newDeployment(namespace, image string, replicas int32) *appsv1.Deployment {
labels := map[string]string{"app": "goapp"}
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "goapp", Namespace: namespace},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "goapp", Image: image}}},
},
},
}
}
57 changes: 57 additions & 0 deletions examples/third_party_integration/ko/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2024 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
http://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 ko

import (
"os"
"testing"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"sigs.k8s.io/e2e-framework/support/kind"
)

var (
testEnv env.Environment
namespace string
kindClusterName string
curDir string
)

func TestMain(m *testing.M) {
c, err := os.Getwd()
if err != nil {
panic(err)
}
curDir = c
cfg, _ := envconf.NewFromFlags()
testEnv = env.NewWithConfig(cfg)
kindClusterName = envconf.RandomName("ko", 16)
namespace = envconf.RandomName("ko", 16)

testEnv.Setup(
envfuncs.CreateCluster(kind.NewProvider(), kindClusterName),
envfuncs.CreateNamespace(namespace),
)

testEnv.Finish(
envfuncs.DeleteNamespace(namespace),
envfuncs.DestroyCluster(kindClusterName),
)
os.Exit(testEnv.Run(m))
}
23 changes: 23 additions & 0 deletions examples/third_party_integration/ko/testdata/example_goapp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2024 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
http://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 main

import "time"

func main() {
time.Sleep(time.Hour)
}
2 changes: 1 addition & 1 deletion third_party/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (m *Manager) run(opts *Opts) (err error) {

// WithPath is used to provide a custom path where the `helm` executable command
// can be found. This is useful in case if your binary is in a non standard location
// and you want to framework to use that instead of retunring an error.
// and you want to framework to use that instead of returning an error.
func (m *Manager) WithPath(path string) *Manager {
m.path = path
return m
Expand Down
Loading

0 comments on commit c1ac086

Please sign in to comment.