Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Revert CS changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sepetrov committed Sep 8, 2020
1 parent c725929 commit 8f25eb8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 111 deletions.
46 changes: 13 additions & 33 deletions pkg/controller/function_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func NewFunctionController(cfg Config, smclient *monitoringv1alpha1.MonitoringV1
if config.Data["enable-build-step"] == "true" {
imagePullSecrets = append(imagePullSecrets, utils.GetSecretsAsLocalObjectReference("kubeless-registry-credentials")...)
}

return &FunctionController{
logger: logrus.WithField("pkg", "function-controller"),
clientset: cfg.KubeCli,
Expand Down Expand Up @@ -278,31 +277,19 @@ func (c *FunctionController) startImageBuildJob(funcObj *kubelessApi.Function, o
return "", false, fmt.Errorf("Unable to parse registry URL: %v", err)
}
image := fmt.Sprintf("%s/%s:%s", regURL.Host, imageName, tag)
if exists {
if !exists {
tlsVerify := true
if c.config.Data["function-registry-tls-verify"] == "false" {
tlsVerify = false
}
err = utils.EnsureFuncImage(c.clientset, funcObj, c.langRuntime, or, imageName, tag, c.config.Data["builder-image"], regURL.Host, imagePullSecret.Name, c.config.Data["provision-image"], tlsVerify, c.imagePullSecrets)
if err != nil {
return "", false, fmt.Errorf("Unable to create image build job: %v", err)
}
} else {
// Image already exists
return image, false, nil
}

tlsVerify := true
if c.config.Data["function-registry-tls-verify"] == "false" {
tlsVerify = false
}
if err = utils.EnsureFuncImage(
c.clientset,
funcObj,
c.langRuntime,
or,
imageName,
tag,
c.config.Data["builder-image"],
regURL.Host,
imagePullSecret.Name,
c.config.Data["provision-image"],
tlsVerify,
c.imagePullSecrets,
); err != nil {
return "", false, fmt.Errorf("Unable to create image build job: %v", err)
}

return image, true, nil
}

Expand Down Expand Up @@ -365,15 +352,8 @@ func (c *FunctionController) ensureK8sResources(funcObj *kubelessApi.Function) e
logrus.Infof("Skipping image-build step for %s", funcObj.ObjectMeta.Name)
}

if err = utils.EnsureFuncDeployment(
c.clientset,
funcObj,
or,
c.langRuntime,
prebuiltImage,
c.config.Data["provision-image"],
c.imagePullSecrets,
); err != nil {
err = utils.EnsureFuncDeployment(c.clientset, funcObj, or, c.langRuntime, prebuiltImage, c.config.Data["provision-image"], c.imagePullSecrets)
if err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/langruntime/langruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ func (l *Langruntimes) GetImageSecrets(runtime string) ([]v1.LocalObjectReferenc

runtimeInf, err := l.findRuntimeVersion(runtime)
if err != nil {
return nil, err
return []v1.LocalObjectReference{}, err
}

if len(runtimeInf.ImagePullSecrets) == 0 {
return nil, nil
return []v1.LocalObjectReference{}, nil
}

for _, s := range runtimeInf.ImagePullSecrets {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func GetAnnotationsFromCRD(clientset clientsetAPIExtensions.Interface, name stri
return crd.GetAnnotations(), nil
}

// GetRandString returns a random string of length N
// GetRandString returns a random string of lenght N
func GetRandString(n int) (string, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
Expand All @@ -459,7 +459,7 @@ func GetRandString(n int) (string, error) {

// GetSecretsAsLocalObjectReference returns a list of LocalObjectReference based on secret names
func GetSecretsAsLocalObjectReference(secrets ...string) []v1.LocalObjectReference {
var res []v1.LocalObjectReference
res := []v1.LocalObjectReference{}
for _, secret := range secrets {
if secret != "" {
res = append(res, v1.LocalObjectReference{Name: secret})
Expand Down
56 changes: 7 additions & 49 deletions pkg/utils/kubelessutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,7 @@ func getChecksum(content string) (string, error) {
// The caller should define the runtime container(s).
// It accepts a prepopulated podSpec with default information and volume that the
// runtime container should mount
func populatePodSpec(
funcObj *kubelessApi.Function,
lr *langruntime.Langruntimes,
podSpec *v1.PodSpec,
runtimeVolumeMount v1.VolumeMount,
provisionImage string,
imagePullSecrets []v1.LocalObjectReference,
) error {
func populatePodSpec(funcObj *kubelessApi.Function, lr *langruntime.Langruntimes, podSpec *v1.PodSpec, runtimeVolumeMount v1.VolumeMount, provisionImage string, imagePullSecrets []v1.LocalObjectReference) error {
depsVolumeName := funcObj.ObjectMeta.Name + "-deps"
result := podSpec
if len(imagePullSecrets) > 0 {
Expand Down Expand Up @@ -480,20 +473,7 @@ func populatePodSpec(
}

// EnsureFuncImage creates a Job to build a function image
func EnsureFuncImage(
client kubernetes.Interface,
funcObj *kubelessApi.Function,
lr *langruntime.Langruntimes,
or []metav1.OwnerReference,
imageName string,
tag string,
builderImage string,
registryHost string,
dockerSecretName string,
provisionImage string,
registryTLSEnabled bool,
imagePullSecrets []v1.LocalObjectReference,
) error {
func EnsureFuncImage(client kubernetes.Interface, funcObj *kubelessApi.Function, lr *langruntime.Langruntimes, or []metav1.OwnerReference, imageName, tag, builderImage, registryHost, dockerSecretName, provisionImage string, registryTLSEnabled bool, imagePullSecrets []v1.LocalObjectReference) error {
if len(tag) < 64 {
return errors.New("Expecting sha256 as image tag")
}
Expand All @@ -508,15 +488,8 @@ func EnsureFuncImage(
RestartPolicy: v1.RestartPolicyOnFailure,
}
runtimeVolumeMount := getRuntimeVolumeMount(funcObj.ObjectMeta.Name)

if err = populatePodSpec(
funcObj,
lr,
&podSpec,
runtimeVolumeMount,
provisionImage,
imagePullSecrets,
); err != nil {
err = populatePodSpec(funcObj, lr, &podSpec, runtimeVolumeMount, provisionImage, imagePullSecrets)
if err != nil {
return err
}

Expand Down Expand Up @@ -628,15 +601,7 @@ func mergeMap(dst, src map[string]string) map[string]string {
}

// EnsureFuncDeployment creates/updates a function deployment
func EnsureFuncDeployment(
client kubernetes.Interface,
funcObj *kubelessApi.Function,
or []metav1.OwnerReference,
lr *langruntime.Langruntimes,
prebuiltRuntimeImage string,
provisionImage string,
imagePullSecrets []v1.LocalObjectReference,
) error {
func EnsureFuncDeployment(client kubernetes.Interface, funcObj *kubelessApi.Function, or []metav1.OwnerReference, lr *langruntime.Langruntimes, prebuiltRuntimeImage, provisionImage string, imagePullSecrets []v1.LocalObjectReference) error {

var err error

Expand Down Expand Up @@ -685,15 +650,8 @@ func EnsureFuncDeployment(
}
// only resolve the image name and build the function if it has not been built already
if dpm.Spec.Template.Spec.Containers[0].Image == "" && prebuiltRuntimeImage == "" {

if err := populatePodSpec(
funcObj,
lr,
&dpm.Spec.Template.Spec,
runtimeVolumeMount,
provisionImage,
imagePullSecrets,
); err != nil {
err := populatePodSpec(funcObj, lr, &dpm.Spec.Template.Spec, runtimeVolumeMount, provisionImage, imagePullSecrets)
if err != nil {
return err
}

Expand Down
38 changes: 13 additions & 25 deletions pkg/utils/kubelessutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,8 @@ func TestEnsureImage(t *testing.T) {
pullSecrets := []v1.LocalObjectReference{
{Name: "creds"},
}
if err := EnsureFuncImage(
clientset,
f1,
lr,
or,
"user/image",
"4840d87600137157493ba43a24f0b4bb6cf524ebbf095ce96c79f85bf5a3ff5a",
"kubeless/builder",
"registry.docker.io",
"registry-creds",
"unzip",
true,
pullSecrets,
); err != nil {
err := EnsureFuncImage(clientset, f1, lr, or, "user/image", "4840d87600137157493ba43a24f0b4bb6cf524ebbf095ce96c79f85bf5a3ff5a", "kubeless/builder", "registry.docker.io", "registry-creds", "unzip", true, pullSecrets)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
jobs, err := clientset.BatchV1().Jobs(ns).List(metav1.ListOptions{})
Expand Down Expand Up @@ -743,7 +731,7 @@ func TestEnsureDeploymentWithoutFuncNorHandler(t *testing.T) {
f2 := getDefaultFunc(funcName, ns)
f2.Spec.Function = ""
f2.Spec.Handler = ""
err := EnsureFuncDeployment(clientset, f2, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f2, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -759,7 +747,7 @@ func TestEnsureDeploymentWithImage(t *testing.T) {
// If the Image has been already provided it should not resolve it
f3 := getDefaultFunc(funcName, ns)
f3.Spec.Deployment.Spec.Template.Spec.Containers[0].Image = "test-image"
err := EnsureFuncDeployment(clientset, f3, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f3, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -779,7 +767,7 @@ func TestEnsureDeploymentWithoutFunc(t *testing.T) {
f4 := getDefaultFunc(funcName, ns)
f4.Spec.Function = ""
f4.Spec.Deps = ""
err := EnsureFuncDeployment(clientset, f4, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f4, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -806,15 +794,15 @@ func TestEnsureUpdateDeployment(t *testing.T) {
f1.Spec.Deployment.Spec.Template.ObjectMeta = metav1.ObjectMeta{
Annotations: funcAnno,
}
err := EnsureFuncDeployment(clientset, f1, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f1, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
f6 := kubelessApi.Function{}
f6 = *f1
f6.Spec.Handler = "foo.bar2"
f6.Spec.Deployment.ObjectMeta.Annotations["new-key"] = "value"
err = EnsureFuncDeployment(clientset, &f6, or, lr, "", "unzip", nil)
err = EnsureFuncDeployment(clientset, &f6, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -832,7 +820,7 @@ func TestAvoidDeploymentOverwrite(t *testing.T) {
},
})
f1 := getDefaultFunc(f1Name, ns)
err := EnsureFuncDeployment(clientset, f1, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f1, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err == nil && strings.Contains(err.Error(), "conflicting object") {
t.Errorf("It should fail because a conflict")
}
Expand All @@ -845,7 +833,7 @@ func TestDeploymentWithUnsupportedRuntime(t *testing.T) {
f7 := getDefaultFunc("func7", ns)
f7.Spec.Deps = "deps"
f7.Spec.Runtime = "cobol"
err := EnsureFuncDeployment(clientset, f7, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f7, or, lr, "", "unzip", []v1.LocalObjectReference{})

if err == nil {
t.Fatal("An error should be thrown")
Expand All @@ -858,7 +846,7 @@ func TestDeploymentWithTimeout(t *testing.T) {
// If a timeout is specified it should set an environment variable FUNC_TIMEOUT
f8 := getDefaultFunc(funcName, ns)
f8.Spec.Timeout = "10"
err := EnsureFuncDeployment(clientset, f8, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f8, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -876,7 +864,7 @@ func TestDeploymentWithPrebuiltImage(t *testing.T) {
clientset, or, ns, lr := prepareDeploymentTest(funcName)
// If a prebuilt image is specified it should not build the function using init containers
f9 := getDefaultFunc(funcName, ns)
err := EnsureFuncDeployment(clientset, f9, or, lr, "user/image:test", "unzip", nil)
err := EnsureFuncDeployment(clientset, f9, or, lr, "user/image:test", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand Down Expand Up @@ -909,7 +897,7 @@ func TestDeploymentWithVolumes(t *testing.T) {
MountPath: "/tmp/test",
},
}
err := EnsureFuncDeployment(clientset, f10, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f10, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -931,7 +919,7 @@ func TestEnsureDeploymentWithAffinityOverridden(t *testing.T) {
// If the Image has been already provided it should not resolve it
f3 := getDefaultFunc(funcName, ns)
f3.Spec.Deployment.Spec.Template.Spec.Affinity = &v1.Affinity{}
err := EnsureFuncDeployment(clientset, f3, or, lr, "", "unzip", nil)
err := EnsureFuncDeployment(clientset, f3, or, lr, "", "unzip", []v1.LocalObjectReference{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand Down

0 comments on commit 8f25eb8

Please sign in to comment.