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

Remove runtime dependency on Build controller #326

Merged
merged 14 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions .ko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
baseImageOverrides:
# TODO(jasonhall): Use build-base in the build-pipeline path, when it's build/released.
github.com/knative/build-pipeline/cmd/creds-init: gcr.io/knative-nightly/github.com/knative/build/build-base:latest
github.com/knative/build-pipeline/cmd/git-init: gcr.io/knative-nightly/github.com/knative/build/build-base:latest
4 changes: 4 additions & 0 deletions .ko.yaml.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
baseImageOverrides:
# TODO(jasonhall): Use build-base in the build-pipeline path, when it's build/released.
github.com/knative/build-pipeline/cmd/creds-init: gcr.io/knative-release/github.com/knative/build/build-base:latest
github.com/knative/build-pipeline/cmd/git-init: gcr.io/knative-release/github.com/knative/build/build-base:latest
20 changes: 4 additions & 16 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import (

clientset "github.com/knative/build-pipeline/pkg/client/clientset/versioned"
pipelineinformers "github.com/knative/build-pipeline/pkg/client/informers/externalversions"
buildclientset "github.com/knative/build/pkg/client/clientset/versioned"
buildinformers "github.com/knative/build/pkg/client/informers/externalversions"
"github.com/knative/pkg/configmap"
"github.com/knative/pkg/signals"
)
Expand Down Expand Up @@ -90,16 +88,10 @@ func main() {
logger.Fatalf("Error building pipeline clientset: %v", err)
}

buildClient, err := buildclientset.NewForConfig(cfg)
if err != nil {
logger.Fatalf("Error building Build clientset: %v", err)
}

configMapWatcher := configmap.NewInformedWatcher(kubeClient, system.Namespace)

opt := reconciler.Options{
KubeClientSet: kubeClient,
BuildClientSet: buildClient,
SharedClientSet: sharedClient,
PipelineClientSet: pipelineClient,
ConfigMapWatcher: configMapWatcher,
Expand All @@ -109,13 +101,12 @@ func main() {

kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeClient, opt.ResyncPeriod)
pipelineInformerFactory := pipelineinformers.NewSharedInformerFactory(pipelineClient, opt.ResyncPeriod)
buildInformerFactory := buildinformers.NewSharedInformerFactory(buildClient, opt.ResyncPeriod)

taskInformer := pipelineInformerFactory.Pipeline().V1alpha1().Tasks()
clusterTaskInformer := pipelineInformerFactory.Pipeline().V1alpha1().ClusterTasks()
taskRunInformer := pipelineInformerFactory.Pipeline().V1alpha1().TaskRuns()
resourceInformer := pipelineInformerFactory.Pipeline().V1alpha1().PipelineResources()
buildInformer := buildInformerFactory.Build().V1alpha1().Builds()
podInformer := kubeInformerFactory.Core().V1().Pods()

pipelineInformer := pipelineInformerFactory.Pipeline().V1alpha1().Pipelines()
pipelineRunInformer := pipelineInformerFactory.Pipeline().V1alpha1().PipelineRuns()
Expand All @@ -126,8 +117,8 @@ func main() {
taskRunInformer,
taskInformer,
clusterTaskInformer,
buildInformer,
resourceInformer,
podInformer,
),
pipelinerun.NewController(opt,
pipelineRunInformer,
Expand All @@ -144,7 +135,6 @@ func main() {

kubeInformerFactory.Start(stopCh)
pipelineInformerFactory.Start(stopCh)
buildInformerFactory.Start(stopCh)
if err := configMapWatcher.Start(stopCh); err != nil {
logger.Fatalf("failed to start configuration manager: %v", err)
}
Expand All @@ -155,8 +145,8 @@ func main() {
taskInformer.Informer().HasSynced,
clusterTaskInformer.Informer().HasSynced,
taskRunInformer.Informer().HasSynced,
buildInformer.Informer().HasSynced,
resourceInformer.Informer().HasSynced,
podInformer.Informer().HasSynced,
} {
if ok := cache.WaitForCacheSync(stopCh, synced); !ok {
logger.Fatalf("failed to wait for cache at index %v to sync", i)
Expand Down
1 change: 1 addition & 0 deletions cmd/creds-init/kodata/HEAD
1 change: 1 addition & 0 deletions cmd/creds-init/kodata/LICENSE
1 change: 1 addition & 0 deletions cmd/creds-init/kodata/VENDOR-LICENSE
41 changes: 41 additions & 0 deletions cmd/creds-init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2018 The Knative 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 (
"flag"

"github.com/knative/build-pipeline/pkg/credentials"
"github.com/knative/build-pipeline/pkg/credentials/dockercreds"
"github.com/knative/build-pipeline/pkg/credentials/gitcreds"
"github.com/knative/pkg/logging"
)

func main() {
flag.Parse()

// ignore atomic level because we are not watching this config for any updates
logger, _ := logging.NewLogger("", "creds-init")
defer logger.Sync()

builders := []credentials.Builder{dockercreds.NewBuilder(), gitcreds.NewBuilder()}
for _, c := range builders {
if err := c.Write(); err != nil {
logger.Fatalf("Error initializing credentials: %v", err)
}
}
logger.Infof("Credentials initialized.")
}
1 change: 1 addition & 0 deletions cmd/git-init/kodata/HEAD
1 change: 1 addition & 0 deletions cmd/git-init/kodata/LICENSE
1 change: 1 addition & 0 deletions cmd/git-init/kodata/VENDOR-LICENSE
93 changes: 93 additions & 0 deletions cmd/git-init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2018 The Knative 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 (
"bytes"
"flag"
"os"
"os/exec"
"path/filepath"

"github.com/knative/pkg/logging"
"go.uber.org/zap"
)

var (
url = flag.String("url", "", "The url of the Git repository to initialize.")
revision = flag.String("revision", "", "The Git revision to make the repository HEAD")
path = flag.String("path", "", "Path of directory under which git repository will be copied")
)

func run(logger *zap.SugaredLogger, cmd string, args ...string) {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output
if err := c.Run(); err != nil {
logger.Errorf("Error running %v %v: %v\n%v", cmd, args, err, output.String())
}
}

func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output

if err := c.Run(); err != nil {
logger.Fatalf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
}
}

func main() {
flag.Parse()
logger, _ := logging.NewLogger("", "git-init")
defer logger.Sync()

// HACK HACK HACK
// Git seems to ignore $HOME/.ssh and look in /root/.ssh for unknown reasons.
// As a workaround, symlink /root/.ssh to where we expect the $HOME to land.
// This means SSH auth only works for our built-in git support, and not
// custom steps.
err := os.Symlink("/builder/home/.ssh", "/root/.ssh")
if err != nil {
logger.Fatalf("Unexpected error creating symlink: %v", err)
}

dir, err := os.Getwd()
if err != nil {
logger.Errorf("Failed to get current dir", err)
}

if *path != "" {
runOrFail(logger, "git", "init", *path)
path := filepath.Join(dir, *path)
if err := os.Chdir(path); err != nil {
logger.Fatalf("Failed to change directory with path %s; err %v", path, err)
}
// update dir variable with new path
dir = path
} else {
run(logger, "git", "init")
}

run(logger, "git", "remote", "add", "origin", *url)
runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")

logger.Infof("Successfully cloned %q @ %q in path %q", *url, *revision, dir)
}
1 change: 1 addition & 0 deletions cmd/nop/kodata/HEAD
1 change: 1 addition & 0 deletions cmd/nop/kodata/LICENSE
1 change: 1 addition & 0 deletions cmd/nop/kodata/VENDOR-LICENSE
10 changes: 8 additions & 2 deletions ...kg/client/clientset/versioned/fake/doc.go → cmd/nop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ 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.
*/
// This package has the automatically generated fake clientset.
package fake

package main

import "fmt"

func main() {
fmt.Println("Build successful")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
In output PR, I am introducing shell based image. We could consider using that image and pass arguments echo Build Successful instead of maintaining nop image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Having this image based on distroless/base means that the image doesn't have a shell at all, which reduces its size and attack surface. I'm not opposed if this becomes difficult to maintain for some reason, but we should bias toward minimal images where possible.

}
3 changes: 3 additions & 0 deletions config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
"-logtostderr",
"-stderrthreshold", "INFO",
"-kubeconfig-writer-image", "github.com/knative/build-pipeline/cmd/kubeconfigwriter",
"-creds-image", "github.com/knative/build-pipeline/cmd/creds-init",
"-git-image", "github.com/knative/build-pipeline/cmd/git-init",
"-nop-image", "github.com/knative/build-pipeline/cmd/nop",
]
volumeMounts:
- name: config-logging
Expand Down
21 changes: 20 additions & 1 deletion hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@ initialize $@
set -o errexit
set -o pipefail

# When building a versioned release, we must use .ko.yaml.release
if (( BRANCH_RELEASE )); then
# KO_CONFIG_PATH expects a path containing a .ko.yaml file
export KO_CONFIG_PATH="$(mktemp -d)"
cp .ko.yaml.release "${KO_CONFIG_PATH}/.ko.yaml"
echo "- Using .ko.yaml.release for base image overrides"
fi

run_validation_tests ./test/presubmit-tests.sh

# Build the release

banner "Building the release"

# Location of the base image for creds-init and git images
readonly PIPELINE_BASE_GCR="${KO_DOCKER_REPO}/github.com/knative/build-pipeline/build-base"

# Build should not try to deploy anything, use a bogus value for cluster.
export K8S_CLUSTER_OVERRIDE=CLUSTER_NOT_SET
export K8S_USER_OVERRIDE=USER_NOT_SET
export DOCKER_REPO_OVERRIDE=DOCKER_NOT_SET

# Build the base image for creds-init and git images.
docker build -t ${PIPELINE_BASE_GCR} -f images/Dockerfile images/

echo "Building build-pipeline"
ko resolve ${KO_FLAGS} -f config/ > ${OUTPUT_YAML}
tag_images_in_yaml ${OUTPUT_YAML}

Expand All @@ -46,7 +61,11 @@ if (( ! PUBLISH_RELEASE )); then
exit 0
fi

publish_yaml ${OUTPUT_YAML}
# Push the base image for creds-init and git images.
echo "Pushing base images to ${PIPELINE_BASE_GCR}"
docker push ${PIPELINE_BASE_GCR}

publish_yaml ${OUTPUT_YAML} ${BUILD_PIPELINE_RELEASE_GCS} ${TAG}

branch_release "Knative Build Pipeline" "${OUTPUT_YAML}"

Expand Down
4 changes: 4 additions & 0 deletions images/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM alpine:latest

RUN apk add --update git openssh-client

8 changes: 4 additions & 4 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ type TaskRunList struct {
Items []TaskRun `json:"items"`
}

// GetBuildRef for task
func (tr *TaskRun) GetBuildRef() corev1.ObjectReference {
// GetBuildPodRef for task
func (tr *TaskRun) GetBuildPodRef() corev1.ObjectReference {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment for the function is referring to old function name

return corev1.ObjectReference{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "Build",
APIVersion: "v1",
Kind: "Pod",
Namespace: tr.Namespace,
Name: tr.Name,
}
Expand Down
Loading