This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ConfigMap glitches and doc registry caching
This fixes a few corner cases in how we handle ConfigMaps for the buildkitd toml config file. It adds integration coverage for those scenarios. It also adds a brief example showing how to configure a local registry for caching purposes to speed up incremental builds on a multi-node cluster.
- Loading branch information
Daniel Hiltgen
committed
Dec 1, 2020
1 parent
efe365a
commit 9438b36
Showing
10 changed files
with
362 additions
and
20 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
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,11 @@ | ||
# Example buildkitd.toml configuration for a local insecure registry | ||
# Initialize buildkit with: | ||
# | ||
# kubectl buildkit create --config ./local-registry-buildkitd.toml | ||
debug = false | ||
[worker.containerd] | ||
namespace = "k8s.io" | ||
[registry."registry:5000"] | ||
http = true | ||
insecure = true | ||
|
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,44 @@ | ||
# Example for running a local registry in your cluster to use for caching purposes | ||
# | ||
# Note: this will not be visible to the underlying container runtime, so you won't | ||
# be able to run images in the cluster from this registry, but you can use it | ||
# as a cache for a multi-node cluster to speed up builds so every builder has access | ||
# to the same cached content | ||
|
||
# TODO explore a variant of this for Host networking, binding to localhost on port 5000 | ||
# and see if that's viable for a local dev registry pattern | ||
|
||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: registry | ||
labels: | ||
app: registry | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: registry | ||
template: | ||
metadata: | ||
labels: | ||
app: registry | ||
spec: | ||
containers: | ||
- name: registry | ||
image: docker.io/registry | ||
ports: | ||
- containerPort: 5000 | ||
|
||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: registry | ||
spec: | ||
type: ClusterIP | ||
selector: | ||
app: registry | ||
ports: | ||
- protocol: TCP | ||
port: 5000 |
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,24 @@ | ||
// Copyright (C) 2020 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package common | ||
|
||
import ( | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// GetKubeClientset retrieves the clientset and namespace | ||
func GetKubeClientset() (*kubernetes.Clientset, string, error) { | ||
configFlags := genericclioptions.NewConfigFlags(true) | ||
clientConfig := configFlags.ToRawKubeConfigLoader() | ||
ns, _, err := clientConfig.Namespace() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
restClientConfig, err := clientConfig.ClientConfig() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
clientset, err := kubernetes.NewForConfig(restClientConfig) | ||
return clientset, ns, err | ||
} |
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,207 @@ | ||
// Copyright (C) 2020 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package suites | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/vmware-tanzu/buildkit-cli-for-kubectl/integration/common" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
v1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
) | ||
|
||
type configMapSuite struct { | ||
suite.Suite | ||
Name string | ||
CreateFlags []string | ||
|
||
ClientSet *kubernetes.Clientset | ||
Namespace string | ||
|
||
configMapClient v1.ConfigMapInterface | ||
} | ||
|
||
func (s *configMapSuite) SetupTest() { | ||
var err error | ||
s.ClientSet, s.Namespace, err = common.GetKubeClientset() | ||
require.NoError(s.T(), err, "%s: kube client failed", s.Name) | ||
s.configMapClient = s.ClientSet.CoreV1().ConfigMaps(s.Namespace) | ||
} | ||
|
||
func (s *configMapSuite) getConfigMap() *corev1.ConfigMap { | ||
payload := `# pre-existing configuration | ||
# nothing to see here... | ||
` | ||
return &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: appsv1.SchemeGroupVersion.String(), | ||
Kind: "ConfigMap", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: s.Namespace, | ||
Name: s.Name, | ||
}, | ||
BinaryData: map[string][]byte{ | ||
"buildkitd.toml": []byte(payload), | ||
}, | ||
} | ||
} | ||
|
||
func (s *configMapSuite) TestDefaultCreate() { | ||
logrus.Infof("%s: Creating builder with default config", s.Name) | ||
args := append( | ||
[]string{ | ||
s.Name, | ||
}, | ||
s.CreateFlags..., | ||
) | ||
err := common.RunBuildkit("create", args) | ||
require.NoError(s.T(), err, "%s: builder create failed", s.Name) | ||
cfg, err := s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.NoError(s.T(), err, "%s: fetch configmap failed", s.Name) | ||
data, ok := cfg.BinaryData["buildkitd.toml"] | ||
require.True(s.T(), ok, "missing buildkitd.toml: %#v", cfg.BinaryData) | ||
// Spot check an expected string | ||
require.Contains(s.T(), string(data), "Default buildkitd configuration.") | ||
|
||
// Tear down the builder | ||
logrus.Infof("%s: Removing builder", s.Name) | ||
err = common.RunBuildkit("rm", []string{ | ||
s.Name, | ||
}) | ||
require.NoError(s.T(), err, "%s: builder rm failed", s.Name) | ||
_, err = s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.Error(s.T(), err, "config map wasn't cleaned up") | ||
require.Contains(s.T(), err.Error(), "not found") | ||
} | ||
|
||
// Pre-create a config and make sure it does not get overridden by the default creation flow | ||
func (s *configMapSuite) TestPreExistingConfigDefaultCreate() { | ||
logrus.Infof("%s: Creating pre-existing config", s.Name) | ||
_, err := s.configMapClient.Create(context.Background(), s.getConfigMap(), metav1.CreateOptions{}) | ||
require.NoError(s.T(), err, "%s: pre-existing configmap create failed", s.Name) | ||
|
||
logrus.Infof("%s: Creating builder with default config", s.Name) | ||
args := append( | ||
[]string{ | ||
s.Name, | ||
}, | ||
s.CreateFlags..., | ||
) | ||
err = common.RunBuildkit("create", args) | ||
require.NoError(s.T(), err, "%s: builder create failed", s.Name) | ||
cfg, err := s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.NoError(s.T(), err, "%s: fetch configmap failed", s.Name) | ||
data, ok := cfg.BinaryData["buildkitd.toml"] | ||
require.True(s.T(), ok, "missing buildkitd.toml: %#v", cfg.BinaryData) | ||
// Spot check an expected string doesn't exist | ||
require.NotContains(s.T(), string(data), "Default buildkitd configuration.") | ||
|
||
// Tear down the builder | ||
logrus.Infof("%s: Removing builder", s.Name) | ||
err = common.RunBuildkit("rm", []string{ | ||
s.Name, | ||
}) | ||
require.NoError(s.T(), err, "%s: builder rm failed", s.Name) | ||
_, err = s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
// TODO if we preserve pre-existing configmaps this will need to be refined. | ||
require.Error(s.T(), err, "config map wasn't cleaned up") | ||
require.Contains(s.T(), err.Error(), "not found") | ||
} | ||
|
||
func (s *configMapSuite) TestCustomCreate() { | ||
logrus.Infof("%s: Creating builder with custom config", s.Name) | ||
dir, cleanup, err := common.NewBuildContext(map[string]string{ | ||
"buildkitd.toml": `# Custom config file | ||
# nothing to see here 2 | ||
`}) | ||
require.NoError(s.T(), err, "%s: config file creation", s.Name) | ||
|
||
defer cleanup() | ||
|
||
args := append( | ||
[]string{ | ||
"--config", filepath.Join(dir, "buildkitd.toml"), | ||
s.Name, | ||
}, | ||
s.CreateFlags..., | ||
) | ||
err = common.RunBuildkit("create", args) | ||
require.NoError(s.T(), err, "%s: builder create failed", s.Name) | ||
cfg, err := s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.NoError(s.T(), err, "%s: fetch configmap failed", s.Name) | ||
data, ok := cfg.BinaryData["buildkitd.toml"] | ||
require.True(s.T(), ok, "missing buildkitd.toml: %#v", cfg.BinaryData) | ||
// Spot check an expected string | ||
require.NotContains(s.T(), string(data), "Default buildkitd configuration.", string(data)) | ||
require.Contains(s.T(), string(data), "Custom config file", string(data)) | ||
|
||
// Tear down the builder | ||
logrus.Infof("%s: Removing builder", s.Name) | ||
err = common.RunBuildkit("rm", []string{ | ||
s.Name, | ||
}) | ||
require.NoError(s.T(), err, "%s: builder rm failed", s.Name) | ||
_, err = s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.Error(s.T(), err, "config map wasn't cleaned up") | ||
require.Contains(s.T(), err.Error(), "not found") | ||
} | ||
func (s *configMapSuite) TestPreExistingWithCustomCreate() { | ||
logrus.Infof("%s: Creating pre-existing config", s.Name) | ||
_, err := s.configMapClient.Create(context.Background(), s.getConfigMap(), metav1.CreateOptions{}) | ||
require.NoError(s.T(), err, "%s: pre-existing configmap create failed", s.Name) | ||
|
||
logrus.Infof("%s: Creating builder with custom config", s.Name) | ||
dir, cleanup, err := common.NewBuildContext(map[string]string{ | ||
"buildkitd.toml": `# Custom config file | ||
# nothing to see here 2 | ||
`}) | ||
require.NoError(s.T(), err, "%s: config file create failed", s.Name) | ||
|
||
defer cleanup() | ||
|
||
args := append( | ||
[]string{ | ||
"--config", filepath.Join(dir, "buildkitd.toml"), | ||
s.Name, | ||
}, | ||
s.CreateFlags..., | ||
) | ||
err = common.RunBuildkit("create", args) | ||
require.NoError(s.T(), err, "%s: builder create failed", s.Name) | ||
cfg, err := s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.NoError(s.T(), err, "%s: fetch configmap failed", s.Name) | ||
data, ok := cfg.BinaryData["buildkitd.toml"] | ||
require.True(s.T(), ok, "missing buildkitd.toml: %#v", cfg.BinaryData) | ||
// Spot check expected strings | ||
require.NotContains(s.T(), string(data), "Default buildkitd configuration.", string(data)) | ||
require.NotContains(s.T(), string(data), "pre-existing configuration", string(data)) | ||
require.Contains(s.T(), string(data), "Custom config file", string(data)) | ||
|
||
// Tear down the builder | ||
logrus.Infof("%s: Removing builder", s.Name) | ||
err = common.RunBuildkit("rm", []string{ | ||
s.Name, | ||
}) | ||
require.NoError(s.T(), err, "%s: builder rm failed", s.Name) | ||
_, err = s.configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{}) | ||
require.Error(s.T(), err, "config map wasn't cleaned up") | ||
require.Contains(s.T(), err.Error(), "not found") | ||
} | ||
|
||
func TestConfigMapSuite(t *testing.T) { | ||
common.Skipper(t) | ||
//t.Parallel() // TODO - tests fail if run in parallel, may be actual race bug | ||
suite.Run(t, &configMapSuite{ | ||
Name: "configmaptest", | ||
}) | ||
} |
Oops, something went wrong.