cfutil

package
v2.11.26 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 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 (
	// DatabaseURLEnvVarName is the environment variable expected by
	// applications looking for a CF style database URI.
	DatabaseURLEnvVarName = "DATABASE_URL"

	// VcapServicesEnvVarName is the environment variable expected by
	// applications looking CF style service injection details.
	VcapServicesEnvVarName = "VCAP_SERVICES"
)
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) map[string]interface{}

CreateVcapApplication creates values for the VCAP_APPLICATION environment variable based on values on the app. These values are merged with the pod values determined at runtime.

Example
package main

import (
	"fmt"

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

func main() {
	app := &v1alpha1.App{}
	app.Name = "my-app"
	app.Namespace = "my-ns"
	app.UID = "12345"
	app.Status.Routes = []v1alpha1.AppRouteStatus{
		{URL: "app.example.com", Status: v1alpha1.RouteBindingStatusOrphaned},
		{URL: "app.example.com", Status: v1alpha1.RouteBindingStatusUnknown},
	}

	envMap := cfutil.CreateVcapApplication(app)

	fmt.Println("VCAP_APPLICATION values:")
	fmt.Println("application_id:", envMap["application_id"])
	fmt.Println("application_name:", envMap["application_name"])
	fmt.Println("name:", envMap["name"])
	fmt.Println("application_uris:", envMap["application_uris"])
	fmt.Println("uris:", envMap["uris"])
	fmt.Println("space_name:", envMap["space_name"])
	fmt.Println("process_id:", envMap["process_id"])
	fmt.Println("process_type:", envMap["process_type"])

}
Output:

VCAP_APPLICATION values:
application_id: 12345
application_name: my-app
name: my-app
application_uris: [app.example.com]
uris: [app.example.com]
space_name: my-ns
process_id: 12345
process_type: web

Types

type SystemEnvInjector

type SystemEnvInjector interface {
	// GetVcapServices gets a VCAP_SERVICES compatible environment variable.
	GetVcapServices(ctx context.Context, appName string, bindings []v1alpha1.ServiceInstanceBinding) ([]VcapService, error)

	// GetVcapService gets a VCAP_SERVICES service entry.
	GetVcapService(ctx context.Context, appName string, binding v1alpha1.ServiceInstanceBinding) (VcapService, error)

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

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

func NewSystemEnvInjector

func NewSystemEnvInjector(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,omitempty"`  // The name assigned to the service binding by the user.
	InstanceName string                     `json:"instance_name,omitempty"` // 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,omitempty"`          // The service plan selected when the service instance was created.
	Credentials  map[string]json.RawMessage `json:"credentials"`             // The service-specific credentials needed to access the service instance.
	VolumeMounts []VolumeMount              `json:"volume_mounts,omitempty"` // Only for VolumeServiceBinding. The volume specific information.
}

VcapService represents a single entry in a VCAP_SERVICES map. It holds the credentials for a single service binding. For user-provided services, many fields are omitted if they are empty, such as binding name, instance name, and plan. If there are no tags for the service, the JSON encoding is an empty list rather than null. See http://engineering.pivotal.io/post/spring-boot-injecting-credentials/ for an example of VCAP_SERVICES for a user-provided service.

func NewVcapService

func NewVcapService(binding kfv1alpha1.ServiceInstanceBinding, credentialsSecret corev1.Secret) VcapService

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

Example
package main

import (
	"encoding/json"
	"fmt"

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

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

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
	val1 := json.RawMessage(`"value1"`)
	val2 := json.RawMessage(`"value2"`)
	credentialsSecret := corev1.Secret{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-creds-secret",
		},
		Data: map[string][]byte{
			"key1": val1,
			"key2": val2,
		},
	}

	binding := v1alpha1.ServiceInstanceBinding{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-binding-generated-name",
		},
		Spec: v1alpha1.ServiceInstanceBindingSpec{
			BindingType: v1alpha1.BindingType{
				App: &v1alpha1.AppRef{
					Name: "my-app",
				},
			},
			InstanceRef: corev1.LocalObjectReference{
				Name: "my-instance",
			},
			BindingNameOverride: "custom-binding-name",
		},
		Status: v1alpha1.ServiceInstanceBindingStatus{
			BindingName: "custom-binding-name",
			CredentialsSecretRef: corev1.LocalObjectReference{
				Name: "my-creds-secret",
			},
			ServiceFields: v1alpha1.ServiceFields{
				Tags:      []string{"mysql"},
				ClassName: "my-service",
				PlanName:  "my-service-plan",
			},
		},
	}

	vs := cfutil.NewVcapService(binding, credentialsSecret)

	fmt.Printf("Name: %s\n", vs.Name)
	fmt.Printf("InstanceName: %s\n", vs.InstanceName)
	fmt.Printf("BindingName: %s\n", *vs.BindingName)
	fmt.Printf("Credentials: %s\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/v2/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

type VolumeMount

type VolumeMount struct {
	// ContainerDir contains the path to the mounted volume that you bound to your App.
	ContainerDir string `json:"container_dir,omitempty"`
	// DeviceType is the NFS volume release. This currently only supports shared devices.
	// A shared device represents a distributed file system that can mount on all App instances simultaneously.
	DeviceType string `json:"device_type,omitempty"`
	// Mode is a string that informs what type of access your App has to NFS, either read-only (`ro`) or read-write (`rw`).
	Mode string `json:"mode,omitempty"`
}

VolumeMount contains volume specific information.

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