service

package
v0.0.0-...-6231fe6 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 10 Imported by: 14

Documentation

Overview

Example (Full)
package main

import (
	"embed"
	"os"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/intstr"

	testlog "knative.dev/reconciler-test/pkg/logging"
	"knative.dev/reconciler-test/pkg/manifest"
	"knative.dev/reconciler-test/pkg/resources/service"
)

//go:embed *.yaml
var templates embed.FS

func main() {
	ctx := testlog.NewContext()
	images := map[string]string{}
	cfg := map[string]interface{}{
		"name":      "foo",
		"namespace": "bar",
	}

	opts := []manifest.CfgFn{
		service.WithLabels(map[string]string{
			"color": "green",
		}),
		service.WithAnnotations(map[string]interface{}{
			"app.kubernetes.io/name": "app",
		}),
		service.WithType(corev1.ServiceTypeClusterIP),
		service.WithSelectors(map[string]string{
			"app.kubernetes.io/name": "foobar",
		}),
		service.WithExternalName("my-external.name"),
		service.WithPorts([]corev1.ServicePort{{
			Port:       1234,
			TargetPort: intstr.FromInt(5678),
		}}),
	}

	for _, opt := range opts {
		opt(cfg)
	}

	files, err := manifest.ExecuteYAML(ctx, templates, images, cfg)
	if err != nil {
		panic(err)
	}

	manifest.OutputYAML(os.Stdout, files)
}
Output:

apiVersion: v1
kind: Service
metadata:
  name: foo
  namespace: bar
  annotations:
    app.kubernetes.io/name: "app"
  labels:
    color: "green"
spec:
  selector:
    app.kubernetes.io/name: "foobar"
  ports:
    - protocol: TCP
      port: 1234
      targetPort: 5678
  type: ClusterIP
  externalName: my-external.name
Example (Min)
package main

import (
	"embed"
	"os"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/intstr"

	testlog "knative.dev/reconciler-test/pkg/logging"
	"knative.dev/reconciler-test/pkg/manifest"
)

//go:embed *.yaml
var templates embed.FS

func main() {
	ctx := testlog.NewContext()
	images := map[string]string{}
	cfg := map[string]interface{}{
		"name":      "foo",
		"namespace": "bar",
		"ports": []corev1.ServicePort{{
			Port:       80,
			TargetPort: intstr.FromInt(8080),
		}},
	}

	files, err := manifest.ExecuteYAML(ctx, templates, images, cfg)
	if err != nil {
		panic(err)
	}

	manifest.OutputYAML(os.Stdout, files)
}
Output:

apiVersion: v1
kind: Service
metadata:
  name: foo
  namespace: bar
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Index

Examples

Constants

This section is empty.

Variables

View Source
var WithAnnotations = manifest.WithAnnotations
View Source
var WithLabels = manifest.WithLabels

Functions

func Address

func Address(ctx context.Context, name string) (*duckv1.Addressable, error)

func AsDestinationRef

func AsDestinationRef(name string) *duckv1.Destination

func AsKReference

func AsKReference(name string) *duckv1.KReference

AsKReference returns a KReference for a Service without namespace.

func AsTrackerReference

func AsTrackerReference(name string) *tracker.Reference

func GVR

func Install

func Install(name string, opts ...manifest.CfgFn) feature.StepFn

Install will create a Service resource. If no ports where defined via the WithPorts option, a default mapping 80:8080 will be used.

func WithExternalName

func WithExternalName(externalName string) manifest.CfgFn

func WithPorts

func WithPorts(ports []corev1.ServicePort) manifest.CfgFn

func WithSelectors

func WithSelectors(selectors map[string]string) manifest.CfgFn

func WithType

func WithType(serviceType corev1.ServiceType) manifest.CfgFn

Types

This section is empty.

Jump to

Keyboard shortcuts

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