Skip to content
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
13 changes: 13 additions & 0 deletions k8s/labels/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package labels

const (
FunctionKey = "function.knative.dev"
FunctionValue = "true"
FunctionRuntimeKey = "function.knative.dev/runtime"
FunctionNameKey = "function.knative.dev/name"

// --- handle usage of deprecated labels
DeprecatedFunctionKey = "boson.dev/function"
DeprecatedFunctionRuntimeKey = "boson.dev/runtime"
// --- end of handling usage of deprecated runtime labels
)
3 changes: 2 additions & 1 deletion k8s/persistent_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetPersistentVolumeClaim(ctx context.Context, name, namespaceOverride strin
return client.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{})
}

func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride string, accessMode corev1.PersistentVolumeAccessMode, resourceRequest resource.Quantity) (err error) {
func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride string, labels map[string]string, accessMode corev1.PersistentVolumeAccessMode, resourceRequest resource.Quantity) (err error) {
client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
if err != nil {
return
Expand All @@ -27,6 +27,7 @@ func CreatePersistentVolumeClaim(ctx context.Context, name, namespaceOverride st
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{accessMode},
Expand Down
5 changes: 3 additions & 2 deletions k8s/role_bidings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func CreateRoleBindingForServiceAccount(ctx context.Context, name, namespaceOverride, serviceAccountName, roleKind, roleName string) (err error) {
func CreateRoleBindingForServiceAccount(ctx context.Context, name, namespaceOverride string, labels map[string]string, serviceAccountName, roleKind, roleName string) (err error) {
client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
if err != nil {
return
}

rb := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: name,
Labels: labels,
},
Subjects: []rbacv1.Subject{
{
Expand Down
3 changes: 2 additions & 1 deletion k8s/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ListSecretsNames(ctx context.Context, namespaceOverride string) (names []st
return
}

func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride, username, password, server string) (err error) {
func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride string, labels map[string]string, username, password, server string) (err error) {
client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
if err != nil {
return
Expand All @@ -46,6 +46,7 @@ func CreateDockerRegistrySecret(ctx context.Context, name, namespaceOverride, us
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Type: corev1.SecretTypeDockerConfigJson,
Data: map[string][]byte{},
Expand Down
3 changes: 2 additions & 1 deletion k8s/service_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride, secretName string) (err error) {
func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride string, labels map[string]string, secretName string) (err error) {
client, namespace, err := NewClientAndResolvedNamespace(namespaceOverride)
if err != nil {
return
Expand All @@ -17,6 +17,7 @@ func CreateServiceAccountWithSecret(ctx context.Context, name, namespaceOverride
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Secrets: []corev1.ObjectReference{
{
Expand Down
13 changes: 9 additions & 4 deletions knative/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/k8s"
"knative.dev/kn-plugin-func/k8s/labels"
)

const LIVENESS_ENDPOINT = "/health/liveness"
Expand Down Expand Up @@ -349,10 +350,14 @@ func updateService(f fn.Function, newEnv []corev1.EnvVar, newEnvFrom []corev1.En
// value: {{ env:MY_ENV }}
func processLabels(f fn.Function) (map[string]string, error) {
labels := map[string]string{
"boson.dev/function": "true",
"boson.dev/runtime": f.Runtime,
"function.knative.dev": "true",
"function.knative.dev/runtime": f.Runtime,
labels.FunctionKey: labels.FunctionValue,
labels.FunctionNameKey: f.Name,
labels.FunctionRuntimeKey: f.Runtime,

// --- handle usage of deprecated labels (`boson.dev/function`, `boson.dev/runtime`)
labels.DeprecatedFunctionKey: labels.FunctionValue,
labels.DeprecatedFunctionRuntimeKey: f.Runtime,
// --- end of handling usage of deprecated runtime labels
}
for _, label := range f.Labels {
if label.Key != nil && label.Value != nil {
Expand Down
17 changes: 5 additions & 12 deletions knative/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ import (

fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/k8s"
)

const (
labelFunctionKey = "function.knative.dev"
deprecatedLabelFunctionKey = "boson.dev/function"
labelFunctionValue = "true"
labelRuntimeKey = "function.knative.dev/runtime"
deprecatedLabelRuntimeKey = "boson.dev/runtime"
"knative.dev/kn-plugin-func/k8s/labels"
)

type Lister struct {
Expand All @@ -43,13 +36,13 @@ func (l *Lister) List(ctx context.Context) (items []fn.ListItem, err error) {
return
}

lst, err := client.ListServices(ctx, clientservingv1.WithLabel(labelFunctionKey, labelFunctionValue))
lst, err := client.ListServices(ctx, clientservingv1.WithLabel(labels.FunctionKey, labels.FunctionValue))
if err != nil {
return
}

// --- handle usage of deprecated function labels (`boson.dev/function`)
lstDeprecated, err := client.ListServices(ctx, clientservingv1.WithLabel(deprecatedLabelFunctionKey, labelFunctionValue))
lstDeprecated, err := client.ListServices(ctx, clientservingv1.WithLabel(labels.DeprecatedFunctionKey, labels.FunctionValue))
if err != nil {
return
}
Expand Down Expand Up @@ -82,10 +75,10 @@ func (l *Lister) List(ctx context.Context) (items []fn.ListItem, err error) {

// --- handle usage of deprecated runtime labels (`boson.dev/runtime`)
runtimeLabel := ""
if val, ok := f.Labels[labelRuntimeKey]; ok {
if val, ok := f.Labels[labels.FunctionRuntimeKey]; ok {
runtimeLabel = val
} else {
runtimeLabel = f.Labels[deprecatedLabelRuntimeKey]
runtimeLabel = f.Labels[labels.DeprecatedFunctionRuntimeKey]
}
// --- end of handling usage of deprecated runtime labels

Expand Down
16 changes: 10 additions & 6 deletions pipelines/tekton/pipeplines_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/docker"
"knative.dev/kn-plugin-func/k8s"
"knative.dev/kn-plugin-func/k8s/labels"
"knative.dev/kn-plugin-func/knative"
"knative.dev/pkg/apis"
)
Expand Down Expand Up @@ -83,14 +84,17 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
return err
}

err = k8s.CreatePersistentVolumeClaim(ctx, getPipelinePvcName(f), pp.namespace, corev1.ReadWriteOnce, *resource.NewQuantity(DefaultPersistentVolumeClaimSize, resource.DecimalSI))
// let's specify labels that will be applied to every resouce that is created for a Pipeline
labels := map[string]string{labels.FunctionNameKey: f.Name}

err = k8s.CreatePersistentVolumeClaim(ctx, getPipelinePvcName(f), pp.namespace, labels, corev1.ReadWriteOnce, *resource.NewQuantity(DefaultPersistentVolumeClaimSize, resource.DecimalSI))
if err != nil {
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("problem creating persistent volume claim: %v", err)
}
}

_, err = client.Pipelines(pp.namespace).Create(ctx, generatePipeline(f), metav1.CreateOptions{})
_, err = client.Pipelines(pp.namespace).Create(ctx, generatePipeline(f, labels), metav1.CreateOptions{})
if err != nil {
if !errors.IsAlreadyExists(err) {
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -118,31 +122,31 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
registry = authn.DefaultAuthKey
}

err = k8s.CreateDockerRegistrySecret(ctx, getPipelineSecretName(f), pp.namespace, creds.Username, creds.Password, registry)
err = k8s.CreateDockerRegistrySecret(ctx, getPipelineSecretName(f), pp.namespace, labels, creds.Username, creds.Password, registry)
if err != nil {
return err
}
} else if err != nil {
return fmt.Errorf("problem in creating secret: %v", err)
}

err = k8s.CreateServiceAccountWithSecret(ctx, getPipelineBuilderServiceAccountName(f), pp.namespace, getPipelineSecretName(f))
err = k8s.CreateServiceAccountWithSecret(ctx, getPipelineBuilderServiceAccountName(f), pp.namespace, labels, getPipelineSecretName(f))
if err != nil {
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("problem in creating service account: %v", err)
}
}

// using ClusterRole `knative-serving-namespaced-admin` that should be present on the cluster after the installation of Knative Serving
err = k8s.CreateRoleBindingForServiceAccount(ctx, getPipelineDeployerRoleBindingName(f), pp.namespace, getPipelineBuilderServiceAccountName(f), "ClusterRole", "knative-serving-namespaced-admin")
err = k8s.CreateRoleBindingForServiceAccount(ctx, getPipelineDeployerRoleBindingName(f), pp.namespace, labels, getPipelineBuilderServiceAccountName(f), "ClusterRole", "knative-serving-namespaced-admin")
if err != nil {
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("problem in creating role biding: %v", err)
}
}

pp.progressListener.Increment("Running Pipeline with the Function")
pr, err := client.PipelineRuns(pp.namespace).Create(ctx, generatePipelineRun(f), metav1.CreateOptions{})
pr, err := client.PipelineRuns(pp.namespace).Create(ctx, generatePipelineRun(f, labels), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("problem in creating pipeline run: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions pipelines/tekton/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
fn "knative.dev/kn-plugin-func"
)

func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {
func generatePipeline(f fn.Function, labels map[string]string) *pplnv1beta1.Pipeline {
pipelineName := getPipelineName(f)

params := []pplnv1beta1.ParamSpec{
Expand Down Expand Up @@ -52,7 +52,8 @@ func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {

return &pplnv1beta1.Pipeline{
ObjectMeta: v1.ObjectMeta{
Name: pipelineName,
Name: pipelineName,
Labels: labels,
},
Spec: pplnv1beta1.PipelineSpec{
Params: params,
Expand All @@ -62,7 +63,7 @@ func generatePipeline(f fn.Function) *pplnv1beta1.Pipeline {
}
}

func generatePipelineRun(f fn.Function) *pplnv1beta1.PipelineRun {
func generatePipelineRun(f fn.Function, labels map[string]string) *pplnv1beta1.PipelineRun {

revision := ""
if f.Git.Revision != nil {
Expand All @@ -76,6 +77,7 @@ func generatePipelineRun(f fn.Function) *pplnv1beta1.PipelineRun {
return &pplnv1beta1.PipelineRun{
ObjectMeta: v1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-run-", getPipelineName(f)),
Labels: labels,
},

Spec: pplnv1beta1.PipelineRunSpec{
Expand Down