cfutil

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cfutil is for utilities around parsing and generating CF compatible things like application manifests and VCAP variables.

These are reimplemented rather than imported so kf can provide additional context and warnings when there are incompatibilities between the platforms.

Index

Examples

Constants

View Source
const (
	// VcapApplicationEnvVarName is the environment variable expected by
	// applications looking for CF style app environment info.
	VcapApplicationEnvVarName = "VCAP_APPLICATION"
)

Variables

This section is empty.

Functions

func CreateVcapApplication

func CreateVcapApplication(app *v1alpha1.App) (corev1.EnvVar, error)

CreateVcapApplication creates a VCAP_APPLICATION style environment variable based on the values on the given service.

Example
package main

import (
	"fmt"

	v1alpha1 "github.com/google/kf/pkg/apis/kf/v1alpha1"
	"github.com/google/kf/pkg/kf/cfutil"
)

func main() {
	app := &v1alpha1.App{}
	app.Name = "my-app"
	app.Namespace = "my-ns"

	env, err := cfutil.CreateVcapApplication(app)
	if err != nil {
		panic(err)
	}

	fmt.Println("Name:", env.Name, "Value:", env.Value)

}
Output:

Name: VCAP_APPLICATION Value: {"application_name":"my-app","name":"my-app","space_name":"my-ns"}

Types

type SystemEnvInjector

type SystemEnvInjector interface {
	// GetVcapServices gets a VCAP_SERVICES compatible environment variable.
	GetVcapServices(appName string, bindings []servicecatalogv1beta1.ServiceBinding) ([]VcapService, error)

	// GetVcapService gets a VCAP_SERVICES service entry.
	GetVcapService(appName string, binding *servicecatalogv1beta1.ServiceBinding) (VcapService, error)

	// ComputeSystemEnv computes the environment variables that should be injected
	// on a given service.
	ComputeSystemEnv(app *v1alpha1.App, serviceBindings []servicecatalogv1beta1.ServiceBinding) (computed []corev1.EnvVar, err error)

	// GetClassFromInstance gets the service class for the given instance.
	GetClassFromInstance(instance *servicecatalogv1beta1.ServiceInstance) (*servicecatalogv1beta1.CommonServiceClassSpec, error)
}

SystemEnvInjector is a utility used to update v1alpha1.Apps with CF style system environment variables like VCAP_SERVICES.

func NewSystemEnvInjector

func NewSystemEnvInjector(
	client servicecatalogclient.Interface,
	k8sclient kubernetes.Interface) SystemEnvInjector

NewSystemEnvInjector creates a utility used to update v1alpha1.Apps with CF style system environment variables like VCAP_SERVICES.

type VcapService

type VcapService struct {
	BindingName  string            `json:"binding_name"`  // The name assigned to the service binding by the user.
	InstanceName string            `json:"instance_name"` // The name assigned to the service instance by the user.
	Name         string            `json:"name"`          // The binding_name if it exists; otherwise the instance_name.
	Label        string            `json:"label"`         // The name of the service offering.
	Tags         []string          `json:"tags"`          // An array of strings an app can use to identify a service instance.
	Plan         string            `json:"plan"`          // The service plan selected when the service instance was created.
	Credentials  map[string]string `json:"credentials"`   // The service-specific credentials needed to access the service instance.
}

VcapService represents a single entry in a VCAP_SERVICES map. It holds the credentials for a single service binding.

func NewVcapService

NewVcapService creates a new VcapService given a binding and associated secret.

Example
package main

import (
	"fmt"

	"github.com/google/kf/pkg/kf/cfutil"

	apiv1beta1 "github.com/poy/service-catalog/pkg/apis/servicecatalog/v1beta1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	instance := apiv1beta1.ServiceInstance{}
	instance.Name = "my-instance"
	instance.Spec.ServiceClassExternalName = "my-service"
	instance.Spec.ServicePlanExternalName = "my-service-plan"

	binding := apiv1beta1.ServiceBinding{}
	binding.Spec.InstanceRef.Name = "my-instance"
	binding.Name = "my-binding"
	binding.Labels = map[string]string{
		kfv1alpha1.ComponentLabel: "custom-binding-name",
	}

	secret := corev1.Secret{}
	secret.Data = map[string][]byte{
		"key1": []byte("value1"),
		"key2": []byte("value2"),
	}

	class := apiv1beta1.CommonServiceClassSpec{
		Tags: []string{"mysql"},
	}

	vs := cfutil.NewVcapService(class, instance, binding, &secret)

	fmt.Printf("Name: %s\n", vs.Name)
	fmt.Printf("InstanceName: %s\n", vs.InstanceName)
	fmt.Printf("BindingName: %s\n", vs.BindingName)
	fmt.Printf("Credentials: %v\n", vs.Credentials)
	fmt.Printf("Service: %v\n", vs.Label)
	fmt.Printf("Plan: %v\n", vs.Plan)
	fmt.Printf("Tags: %v\n", vs.Tags)

}
Output:

Name: custom-binding-name
InstanceName: my-instance
BindingName: custom-binding-name
Credentials: map[key1:value1 key2:value2]
Service: my-service
Plan: my-service-plan
Tags: [mysql]

type VcapServicesMap

type VcapServicesMap map[string][]VcapService

VcapServicesMap mimics CF's VCAP_SERVICES environment variable. See https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES for more information about the structure. The key for each service is the same as the value of the "label" attribute.

func GetVcapServicesMap

func GetVcapServicesMap(appName string, services []VcapService) (VcapServicesMap, error)

func (VcapServicesMap) Add

func (vm VcapServicesMap) Add(service VcapService)

Add inserts the VcapService to the map.

Example
package main

import (
	"fmt"

	"github.com/google/kf/pkg/kf/cfutil"
)

func main() {
	m := cfutil.VcapServicesMap{}
	m.Add(cfutil.VcapService{InstanceName: "instance-a", Label: "foo"})
	m.Add(cfutil.VcapService{InstanceName: "instance-b", Label: "foo"})

	// Elements are registered by their Label.
	fmt.Printf("Number of bindings: %d\n", len(m))
	fmt.Printf("Binding 0: %s, Instance: %s\n", m["foo"][0].Label, m["foo"][0].InstanceName)
	fmt.Printf("Binding 1: %s, Instance: %s\n", m["foo"][1].Label, m["foo"][1].InstanceName)

}
Output:

Number of bindings: 1
Binding 0: foo, Instance: instance-a
Binding 1: foo, Instance: instance-b

Directories

Path Synopsis
Package fake is a generated GoMock package.
Package fake is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL