generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from heylongdacoder/third-party-ko
third_party/ko: Add ko as third party tools
- Loading branch information
Showing
6 changed files
with
390 additions
and
2 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,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}}}, | ||
}, | ||
}, | ||
} | ||
} |
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,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
23
examples/third_party_integration/ko/testdata/example_goapp/main.go
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,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) | ||
} |
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
Oops, something went wrong.