kube

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DocTemplate = template.Must(template.New("kube_doc").Funcs(templates.Funcs).Parse(`
// +k8s:deepcopy-gen=package,register

/* go:generate command for Kubernetes code-generator currently disabled, run the following manually (or uncomment and remove the minus):
	
- //go:generate bash ../../../hack/update-codegen.sh

*/

// Package {{ .ProjectConfig.Version }} is the {{ .ProjectConfig.Version }} version of the API.
// +groupName={{ .ProjectConfig.Name }}
package {{ .ProjectConfig.Version }}

`))
View Source
var GenerateScriptTemplate = template.Must(template.New("kube_generate").Funcs(templates.Funcs).Parse(`
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
ROOT_PKG={{ .ProjectConfig.GoPackage }}
CLIENT_PKG=${ROOT_PKG}/kube/client
APIS_PKG=${ROOT_PKG}/kube/apis

# Below code is copied from https://github.com/weaveworks/flagger/blob/master/hack/update-codegen.sh
CODEGEN_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/code-generator)
# With k8s.io/code-generator v0.28.x the boilerplate file has been removed. So we get it from k8s.io/gengo instead
GENGO_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/gengo)

echo ">> Using ${CODEGEN_PKG}"

# code-generator does work with go.mod but makes assumptions about
# the project living in ` + "$GOPATH/src" + `. To work around this and support
# any location; create a temporary directory, use this as an output
# base, and copy everything back once generated.
TEMP_DIR=$(mktemp -d)
cleanup() {
    echo ">> Removing ${TEMP_DIR}"
    rm -rf ${TEMP_DIR}
}
trap "cleanup" EXIT SIGINT

echo ">> Temporary output directory ${TEMP_DIR}"

# TODO: generate-groups.sh has been deprecated. Move to kube_codegen.sh once https://github.com/kubernetes/code-generator/issues/165 is resolved
# Ensure we can execute.
chmod +x ${CODEGEN_PKG}/generate-groups.sh
chmod +x ${CODEGEN_PKG}/generate-internal-groups.sh

${CODEGEN_PKG}/generate-groups.sh all \
    ${CLIENT_PKG} \
    ${APIS_PKG} \
    {{ .ProjectConfig.Name }}:{{ .ProjectConfig.Version }} \
    --output-base "${TEMP_DIR}" --go-header-file "${GENGO_PKG}/boilerplate/boilerplate.go.txt"
# Copy everything back.
cp -a "${TEMP_DIR}/${ROOT_PKG}/." "${SCRIPT_ROOT}/.."

`))

TODO(marco): replace hardcoded types

View Source
var RegisterTemplate = template.Must(template.New("kube_doc").Funcs(templates.Funcs).Parse(`package {{ .ProjectConfig.Version }}

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	runtime "k8s.io/apimachinery/pkg/runtime"
	schema "k8s.io/apimachinery/pkg/runtime/schema"
)

var (
	// Package-wide variables from generator "register".
	SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "{{ .ProjectConfig.Version }}"}
	SchemeBuilder      = runtime.NewSchemeBuilder(addKnownTypes)
	localSchemeBuilder = &SchemeBuilder
	AddToScheme        = localSchemeBuilder.AddToScheme
)

const (
	// Package-wide consts from generator "register".
	GroupName = "{{ .ProjectConfig.Name }}"
)

func Resource(resource string) schema.GroupResource {
	return SchemeGroupVersion.WithResource(resource).GroupResource()
}

{{ $project := . }}

func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
{{- range .Resources }}
{{- if resourceBelongsToProject $project . }}
		&{{ .Name }}{},
		&{{ .Name }}List{},
{{- end }}
{{- end }}
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

`))
View Source
var TypesTemplate = template.Must(template.New("kube_types").Funcs(templates.Funcs).Parse(`package {{ .ProjectConfig.Version }}


import (
	"encoding/json"
	"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
	"github.com/solo-io/solo-kit/pkg/utils/protoutils"

	api "{{ .ProjectConfig.GoPackage }}"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type metaOnly struct {
	v1.TypeMeta   {{ backtick }}json:",inline"{{ backtick }}
	v1.ObjectMeta {{ backtick }}json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"{{ backtick }}
}

{{- range .Resources}}
{{- if resourceBelongsToProject $ . }}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +resourceName={{ lowercase (upper_camel .PluralName) }}
{{- if .ClusterScoped }}
// +genclient:nonNamespaced
{{- else }}
// +genclient
{{- end }}
{{- if not .HasStatus }}
// +genclient:noStatus
{{- end }}
type {{ .Name }} struct {
	v1.TypeMeta {{ backtick }}json:",inline"{{ backtick }}
	// +optional
	v1.ObjectMeta {{ backtick }}json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"{{ backtick }}

	// Spec defines the implementation of this definition.
	// +optional
	Spec api.{{ .Name }} {{ backtick }}json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"{{ backtick }}

{{- if .HasStatus }}
	Status core.NamespacedStatuses {{ backtick }}json:"status,omitempty" protobuf:"bytes,3,opt,name=status"{{ backtick }}
{{- end }}
}


func (o *{{ .Name }}) MarshalJSON() ([]byte, error) {
	spec, err := protoutils.MarshalMap(&o.Spec)
	if err != nil {
		return nil, err
	}
	delete(spec, "metadata")
{{- if .HasStatus }}
	delete(spec, "namespacedStatuses")
{{- end }}
	asMap := map[string]interface{}{
		"metadata":   o.ObjectMeta,
		"apiVersion": o.TypeMeta.APIVersion,
		"kind":       o.TypeMeta.Kind,
{{- if .HasStatus }}
		"status":     o.Status,
{{- end }}
		"spec":       spec,
	}
	return json.Marshal(asMap)
}

func (o *{{ .Name }}) UnmarshalJSON(data []byte) error {
	var metaOnly metaOnly
	if err := json.Unmarshal(data, &metaOnly); err != nil {
		return err
	}
	var spec api.{{ .Name }}
	if err := protoutils.UnmarshalResource(data, &spec); err != nil {
		return err
	}
	spec.Metadata = nil
	*o = {{ .Name }}{
		ObjectMeta: metaOnly.ObjectMeta,
		TypeMeta:   metaOnly.TypeMeta,
		Spec:       spec,
	}
{{- if .HasStatus }}
	if spec.GetNamespacedStatuses() != nil {
		o.Status = *spec.NamespacedStatuses
		o.Spec.NamespacedStatuses = nil
	}
{{- end }}

	return nil
}


// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// {{ .Name }}List is a collection of {{ .Name }}s.
type {{ .Name }}List struct {
	v1.TypeMeta {{ backtick }}json:",inline"{{ backtick }}
	// +optional
	v1.ListMeta {{ backtick }}json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"{{ backtick }}
	Items       []{{ .Name }} {{ backtick }}json:"items" protobuf:"bytes,2,rep,name=items"{{ backtick }}
}

{{- end }}
{{- end }}
`))
View Source
var VerifyGenerateTemplate = template.Must(template.New("kube_verify").Funcs(templates.Funcs).Parse(`
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..

CI_ROOT=$(dirname "${BASH_SOURCE[0]}")

DIFFROOT="${SCRIPT_ROOT}/pkg"
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg"
_tmp="${SCRIPT_ROOT}/_tmp"

cleanup() {
  rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT

cleanup

mkdir -p "${TMP_DIFFROOT}"
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"

"${CI_ROOT}/update-codegen.sh"
echo "diffing ${DIFFROOT} against freshly generated codegen"
ret=0
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$?
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}"
if [[ $ret -eq 0 ]]
then
  echo "${DIFFROOT} up to date."
else
  echo "${DIFFROOT} is out of date. Please run 'make code-gen'"
  exit 1
fi

`))

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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