forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the initial implementation for the ko builder. Read more about the ko builder in the [design proposal](docs/design_proposals/ko-builder.md). Tracking: GoogleContainerTools#6041
- Loading branch information
Showing
14 changed files
with
590 additions
and
4 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,9 @@ | ||
### Example: ko | ||
|
||
This is an example demonstrating building a Go app with the | ||
[ko](https://github.com/google/ko) builder. | ||
|
||
The included [Cloud Build](https://cloud.google.com/build/docs) configuration | ||
file shows how users can set up a simple pipeline using `skaffold build` and | ||
`skaffold deploy`, without having to create a custom builder image containing | ||
additional tools. |
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,49 @@ | ||
# Demonstrate build & deploy using ko builder. | ||
# Note that no custom images are required for any additional tools. | ||
|
||
options: | ||
dynamic_substitutions: true | ||
env: | ||
- 'KUBECONFIG=/workspace/.kubeconfig' | ||
- 'SKAFFOLD_DEFAULT_REPO=$_IMAGE_REPO' | ||
- 'SKAFFOLD_INTERACTIVE=false' | ||
- 'SKAFFOLD_TIMESTAMPS=true' | ||
- 'SKAFFOLD_UPDATE_CHECK=false' | ||
- 'SKAFFOLD_VERBOSITY=info' | ||
|
||
steps: | ||
- id: build | ||
name: $_SKAFFOLD_IMAGE | ||
entrypoint: skaffold | ||
args: | ||
- build | ||
- --file-output=artifacts.json | ||
|
||
- id: creds | ||
name: $_SKAFFOLD_IMAGE | ||
entrypoint: gcloud | ||
args: | ||
- container | ||
- clusters | ||
- get-credentials | ||
- $_GKE_CLUSTER_NAME | ||
- --project=$_GKE_CLUSTER_PROJECT_ID | ||
- --zone=$_GKE_CLUSTER_ZONE | ||
|
||
- id: deploy | ||
name: $_SKAFFOLD_IMAGE | ||
entrypoint: skaffold | ||
args: | ||
- deploy | ||
- --build-artifacts=artifacts.json | ||
- --detect-minikube=false | ||
- --status-check=true | ||
|
||
substitutions: | ||
_GKE_CLUSTER_NAME: skaffold-ko | ||
_GKE_CLUSTER_PROJECT_ID: $PROJECT_ID | ||
_GKE_CLUSTER_ZONE: us-central1-f | ||
_IMAGE_REPO: gcr.io/${PROJECT_ID} | ||
_SKAFFOLD_IMAGE: gcr.io/k8s-skaffold/skaffold | ||
|
||
timeout: 1200s |
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,3 @@ | ||
module github.com/GoogleContainerTools/skaffold/examples/ko | ||
|
||
go 1.13 |
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,31 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: web | ||
spec: | ||
ports: | ||
- name: http | ||
port: 80 | ||
targetPort: 8080 | ||
selector: | ||
app: web | ||
type: ClusterIP | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: web | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: web | ||
template: | ||
metadata: | ||
labels: | ||
app: web | ||
spec: | ||
containers: | ||
- image: skaffold-ko | ||
name: web | ||
ports: | ||
- containerPort: 8080 |
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,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", hello) | ||
|
||
log.Println("Listening on port 8080") | ||
http.ListenAndServe(":8080", nil) | ||
} | ||
|
||
func hello(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "Hello, World!") | ||
} |
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,6 @@ | ||
apiVersion: skaffold/v2beta17 | ||
kind: Config | ||
build: | ||
artifacts: | ||
- image: skaffold-ko | ||
ko: {} |
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,89 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 integration | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/docker/client" | ||
|
||
// latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko" | ||
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko/schema" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestBuildAndSideloadKoImage(t *testing.T) { | ||
exampleDir, err := koExampleDir() | ||
if err != nil { | ||
t.Fatalf("could not get ko example app dir: %+v", err) | ||
} | ||
imageNameWithTag := "gcr.io/project-id/skaffold-ko:tag" | ||
wantImageID := "imageID" | ||
|
||
api := (&testutil.FakeAPIClient{}).Add(imageNameWithTag, wantImageID) | ||
localDocker := fakeLocalDaemon(api) | ||
pushImages := false | ||
b := ko.NewArtifactBuilder(localDocker, pushImages) | ||
|
||
artifact := &latestV1.Artifact{ | ||
ImageName: "ko://github.com/GoogleContainerTools/skaffold/examples/ko", | ||
Workspace: exampleDir, | ||
ArtifactType: latestV1.ArtifactType{ | ||
KoArtifact: &latestV1.KoArtifact{}, | ||
}, | ||
Dependencies: []*latestV1.ArtifactDependency{}, | ||
} | ||
var imageNameBuffer bytes.Buffer | ||
imageID, err := b.Build(context.TODO(), &imageNameBuffer, artifact, imageNameWithTag) | ||
if err != nil { | ||
t.Fatalf("error during build: %+v", err) | ||
} | ||
|
||
if imageID != wantImageID { | ||
t.Errorf("got image ID %s, wanted %s", imageID, wantImageID) | ||
} | ||
imageName := imageNameBuffer.String() | ||
wantImageNamePrefix := "gcr.io/project-id/skaffold-ko:" | ||
if !strings.HasPrefix(imageName, wantImageNamePrefix) { | ||
t.Errorf("got image name %s, wanted image name with prefix %s", imageName, wantImageNamePrefix) | ||
} | ||
} | ||
|
||
func koExampleDir() (string, error) { | ||
_, filename, _, ok := runtime.Caller(0) | ||
if !ok { | ||
return "", fmt.Errorf("could not get current filename") | ||
} | ||
basepath := filepath.Dir(filename) | ||
exampleDir, err := filepath.Abs(filepath.Join(basepath, "examples", "ko")) | ||
if err != nil { | ||
return "", fmt.Errorf("could not get absolute path of example from basepath %q: %w", basepath, err) | ||
} | ||
return exampleDir, nil | ||
} | ||
|
||
func fakeLocalDaemon(api client.CommonAPIClient) docker.LocalDaemon { | ||
return docker.NewLocalDaemon(api, nil, false, nil) | ||
} |
Oops, something went wrong.