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

add owner reference to ingress and tlssecert metadata #2841

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions pkg/devfile/adapters/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
devfileParser "github.com/openshift/odo/pkg/devfile/parser"
"github.com/openshift/odo/pkg/envinfo"
)

// AdapterContext is a construct that is common to all adapters
Expand All @@ -25,12 +26,13 @@ type Storage struct {

// PushParameters is a struct containing the parameters to be used when pushing to a devfile component
type PushParameters struct {
Path string // Path refers to the parent folder containing the source code to push up to a component
WatchFiles []string // Optional: WatchFiles is the list of changed files detected by odo watch. If empty or nil, odo will check .odo/odo-file-index.json to determine changed files
WatchDeletedFiles []string // Optional: WatchDeletedFiles is the list of deleted files detected by odo watch. If empty or nil, odo will check .odo/odo-file-index.json to determine deleted files
IgnoredFiles []string // IgnoredFiles is the list of files to not push up to a component
ForceBuild bool // ForceBuild determines whether or not to push all of the files up to a component or just files that have changed, added or removed.
Show bool // Show tells whether the devfile command output should be shown on stdout
DevfileBuildCmd string // DevfileBuildCmd takes the build command through the command line and overwrites devfile build command
DevfileRunCmd string // DevfileRunCmd takes the run command through the command line and overwrites devfile run command
Path string // Path refers to the parent folder containing the source code to push up to a component
WatchFiles []string // Optional: WatchFiles is the list of changed files detected by odo watch. If empty or nil, odo will check .odo/odo-file-index.json to determine changed files
WatchDeletedFiles []string // Optional: WatchDeletedFiles is the list of deleted files detected by odo watch. If empty or nil, odo will check .odo/odo-file-index.json to determine deleted files
IgnoredFiles []string // IgnoredFiles is the list of files to not push up to a component
ForceBuild bool // ForceBuild determines whether or not to push all of the files up to a component or just files that have changed, added or removed.
Show bool // Show tells whether the devfile command output should be shown on stdout
DevfileBuildCmd string // DevfileBuildCmd takes the build command through the command line and overwrites devfile build command
DevfileRunCmd string // DevfileRunCmd takes the run command through the command line and overwrites devfile run command
EnvSpecificInfo envinfo.EnvSpecificInfo // EnvSpecificInfo contains infomation of env.yaml file
}
12 changes: 10 additions & 2 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/fatih/color"
"github.com/golang/glog"
"github.com/pkg/errors"

"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/storage"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/utils"
Expand All @@ -22,8 +23,10 @@ import (
versionsCommon "github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/sync"
"github.com/openshift/odo/pkg/util"
"github.com/pkg/errors"
)

// New instantiantes a component adapter
Expand Down Expand Up @@ -88,6 +91,11 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
return errors.Wrapf(err, "unable to get pod for component %s", a.ComponentName)
}

err = component.ApplyConfig(nil, &a.Client, config.LocalConfigInfo{}, parameters.EnvSpecificInfo, color.Output, componentExists)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed.")
}

// Compare the name of the pod with the one before the rollout. If they differ, it means there's a new pod and a force push is required
if componentExists && podName != pod.GetName() {
podChanged = true
Expand Down
20 changes: 7 additions & 13 deletions pkg/kclient/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kclient
import (
"fmt"

componentlabels "github.com/openshift/odo/pkg/component/labels"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -12,27 +11,22 @@ import (
// CreateTLSSecret creates a TLS Secret with the given certificate and private key
// serviceName is the name of the service for the target reference
// ingressDomain is the ingress domain to use for the ingress
func (c *Client) CreateTLSSecret(tlsCertificate []byte, tlsPrivKey []byte, componentName string, applicationName string) (*corev1.Secret, error) {
if componentName == "" {
return nil, fmt.Errorf("componentName name is empty")
func (c *Client) CreateTLSSecret(tlsCertificate []byte, tlsPrivKey []byte, objectMeta metav1.ObjectMeta) (*corev1.Secret, error) {
if objectMeta.Name == "" {
return nil, fmt.Errorf("tlsSecret name is empty")
}
labels := componentlabels.GetLabels(componentName, applicationName, true)
tlsSecretName := componentName + "-tlssecret"
data := make(map[string][]byte)
data["tls.crt"] = tlsCertificate
data["tls.key"] = tlsPrivKey
secretTemplate := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: tlsSecretName,
Labels: labels,
},
Type: corev1.SecretTypeTLS,
Data: data,
ObjectMeta: objectMeta,
Type: corev1.SecretTypeTLS,
Data: data,
}

secret, err := c.KubeClient.CoreV1().Secrets(c.Namespace).Create(&secretTemplate)
if err != nil {
return nil, errors.Wrapf(err, "unable to create secret %s", tlsSecretName)
return nil, errors.Wrapf(err, "unable to create secret %s", objectMeta.Name)
}
return secret, nil
}
57 changes: 35 additions & 22 deletions pkg/kclient/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,48 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

ktesting "k8s.io/client-go/testing"
)

func TestCreateTLSSecret(t *testing.T) {

tests := []struct {
name string
secretName string
componentName string
host string
wantErr bool
name string
objectMeta metav1.ObjectMeta
host string
wantErr bool
}{
{
name: "Case: Valid Component name",
componentName: "testComponent",
secretName: "testComponent-tlssecret",
host: "1.2.3.4.nip.io",
wantErr: false,
name: "Case: Valid Secret name",
objectMeta: metav1.ObjectMeta{
Name: "testComponent-tlssecret",
OwnerReferences: []v1.OwnerReference{
metav1.OwnerReference{
APIVersion: "1",
Kind: "fakeOwnerReference",
Name: "testDeployment",
},
},
},
host: "1.2.3.4.nip.io",
wantErr: false,
},
{
name: "Case: Invalid Component name",
secretName: "testComponent-tlssecret",
componentName: "",
host: "1.2.3.4.nip.io",
wantErr: true,
name: "Case: Invalid Secret name",
objectMeta: metav1.ObjectMeta{
Name: "",
OwnerReferences: []v1.OwnerReference{
metav1.OwnerReference{
APIVersion: "1",
Kind: "fakeOwnerReference",
Name: "testDeployment",
},
},
},
host: "1.2.3.4.nip.io",
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -42,17 +57,15 @@ func TestCreateTLSSecret(t *testing.T) {

fkclientset.Kubernetes.PrependReactor("create", "secrets", func(action ktesting.Action) (bool, runtime.Object, error) {
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: tt.secretName,
},
ObjectMeta: tt.objectMeta,
}
return true, &secret, nil
})
selfsignedcert, err := GenerateSelfSignedCertificate(tt.host)
if err != nil {
t.Errorf("fkclient.GenerateSelfSignedCertificate unexpected error %v", err)
}
createdTLSSceret, err := fkclient.CreateTLSSecret(selfsignedcert.CertPem, selfsignedcert.KeyPem, tt.componentName, "")
createdTLSSceret, err := fkclient.CreateTLSSecret(selfsignedcert.CertPem, selfsignedcert.KeyPem, tt.objectMeta)
// Checks for unexpected error cases
if !tt.wantErr == (err != nil) {
t.Errorf("fkclient.CreateIngress unexpected error %v, wantErr %v", err, tt.wantErr)
Expand All @@ -61,8 +74,8 @@ func TestCreateTLSSecret(t *testing.T) {
if len(fkclientset.Kubernetes.Actions()) != 1 {
t.Errorf("expected 1 action, got: %v", fkclientset.Kubernetes.Actions())
} else {
if createdTLSSceret.Name != tt.secretName {
t.Errorf("secret name does not match the expected name, expected: %s, got %s", tt.secretName, createdTLSSceret.Name)
if createdTLSSceret.Name != tt.objectMeta.Name {
t.Errorf("secret name does not match the expected name, expected: %s, got %s", tt.objectMeta.Name, createdTLSSceret.Name)
}
}
}
Expand Down
12 changes: 1 addition & 11 deletions pkg/odo/cli/component/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import (
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/util"
"github.com/pkg/errors"

Expand Down Expand Up @@ -77,18 +73,12 @@ func (po *PushOptions) DevfilePush() (err error) {
if err != nil {
return err
}

po.Context.KClient.Namespace = po.namespace
err = component.ApplyConfig(nil, po.Context.KClient, config.LocalConfigInfo{}, *po.EnvSpecificInfo, color.Output, po.doesComponentExist)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed.")
}

pushParams := common.PushParameters{
Path: po.sourcePath,
IgnoredFiles: po.ignores,
ForceBuild: po.forceBuild,
Show: po.show,
EnvSpecificInfo: *po.EnvSpecificInfo,
DevfileBuildCmd: strings.ToLower(po.devfileBuildCommand),
DevfileRunCmd: strings.ToLower(po.devfileRunCommand),
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/odo/genericclioptions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ func resolveProject(command *cobra.Command, client *occlient.Client, localConfig
// check that the specified project exists
_, err = project.Exists(client, namespace)
if err != nil {
errFormat := fmt.Sprintf("You don't have permission to create or set project '%s' or the project doesn't exist. Please create or set a different project\n\t", namespace)
e1 := fmt.Sprintf("You don't have permission to create or set project '%s' or the project doesn't exist. Please create or set a different project\n\t", namespace)
errFormat := fmt.Sprint(e1, "%s project create|set <project_name>")
checkProjectCreateOrDeleteOnlyOnInvalidNamespace(command, errFormat)
}
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/golang/glog"
iextensionsv1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down Expand Up @@ -118,6 +119,11 @@ func Create(client *occlient.Client, kClient *kclient.Client, urlName string, po
if experimental.IsExperimentalModeEnabled() {
serviceName := componentName
ingressDomain := fmt.Sprintf("%v.%v", urlName, host)
deployment, err := kClient.GetDeploymentByName(componentName)
if err != nil {
return "", err
}
ownerReference := kclient.GenerateOwnerReference(deployment)
if secureURL {
if len(secretName) != 0 {
_, err := kClient.KubeClient.CoreV1().Secrets(kClient.Namespace).Get(secretName, metav1.GetOptions{})
Expand All @@ -128,13 +134,22 @@ func Create(client *occlient.Client, kClient *kclient.Client, urlName string, po
if len(secretName) == 0 {
defaultTLSSecretName := componentName + "-tlssecret"
_, err := kClient.KubeClient.CoreV1().Secrets(kClient.Namespace).Get(defaultTLSSecretName, metav1.GetOptions{})
// create tls secret if it does not exist
if err != nil {
selfsignedcert, err := kclient.GenerateSelfSignedCertificate(host)
if err != nil {
return "", errors.Wrap(err, "unable to generate self-signed certificate for clutser: "+host)
}
// create tls secret
secret, err := kClient.CreateTLSSecret(selfsignedcert.CertPem, selfsignedcert.KeyPem, componentName, applicationName)
secretlabels := componentlabels.GetLabels(componentName, applicationName, true)
objectMeta := metav1.ObjectMeta{
Name: defaultTLSSecretName,
Labels: secretlabels,
OwnerReferences: []v1.OwnerReference{
ownerReference,
},
}
secret, err := kClient.CreateTLSSecret(selfsignedcert.CertPem, selfsignedcert.KeyPem, objectMeta)
if err != nil {
return "", errors.Wrap(err, "unable to create tls secret: "+secret.Name)
}
Expand All @@ -152,6 +167,7 @@ func Create(client *occlient.Client, kClient *kclient.Client, urlName string, po
ingressSpec := kclient.GenerateIngressSpec(ingressParam)
objectMeta := kclient.CreateObjectMeta(componentName, kClient.Namespace, labels, nil)
objectMeta.Name = urlName
objectMeta.OwnerReferences = append(objectMeta.OwnerReferences, ownerReference)
// Pass in the namespace name, link to the service (componentName) and labels to create a ingress
ingress, err := kClient.CreateIngress(objectMeta, *ingressSpec)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/devfile/cmd_devfile_delete_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package devfile

import (
"github.com/openshift/odo/tests/helper"
"os"
"path/filepath"
"time"

"github.com/openshift/odo/tests/helper"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -49,17 +50,16 @@ var _ = Describe("odo devfile delete command tests", func() {
componentName := helper.RandString(6)
helper.CmdShouldPass("odo", "create", "nodejs", "--context", context, "--project", namespace, componentName)

helper.CmdShouldPass("odo", "url", "create", "example", "--host", "1.2.3.4.nip.io")

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)

helper.CmdShouldPass("odo", "delete", "--devfile", "devfile.yaml", "--namespace", namespace, "-f")

oc.WaitAndCheckForExistence("deployments", namespace, 1)
oc.WaitAndCheckForExistence("pods", namespace, 1)
oc.WaitAndCheckForExistence("services", namespace, 1)

// once https://github.com/openshift/odo/issues/2808 is resolved
// create a url also in this scenario
//oc.WaitAndCheckForExistence("ingress", namespace, 1)
oc.WaitAndCheckForExistence("ingress", namespace, 1)
})
})

Expand Down