k8s

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2019 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvUnknown       Env = "unknown"
	EnvGKE               = "gke"
	EnvMinikube          = "minikube"
	EnvDockerDesktop     = "docker-for-desktop"
	EnvMicroK8s          = "microk8s"
	EnvNone              = "none" // k8s not running (not neces. a problem, e.g. if using Tilt x Docker Compose)
)
View Source
const ContainerIDPrefix = "docker://"
View Source
const DefaultNamespace = Namespace("default")
View Source
const MagicTestContainerID = "tilt-testcontainer"

A magic constant. If the docker client returns this constant, we always match even if the container doesn't have the correct image name.

View Source
const ManifestNameLabel = "tilt-manifest"
View Source
const TiltDeployIDLabel = "tilt-deployid"
View Source
const TiltRunIDLabel = "tilt-runid"

Variables

View Source
var TiltRunID = uuid.New().String()

Functions

func ContainerIDFromContainerStatus

func ContainerIDFromContainerStatus(status v1.ContainerStatus) (container.ID, error)

func ContainerMatching

func ContainerMatching(pod *v1.Pod, ref reference.Named) (v1.ContainerStatus, error)

func ContainerNameFromContainerStatus

func ContainerNameFromContainerStatus(status v1.ContainerStatus) container.Name

func ContainerSpecOf

func ContainerSpecOf(pod *v1.Pod, status v1.ContainerStatus) v1.Container

func ExtractPodTemplateSpec added in v0.1.0

func ExtractPodTemplateSpec(obj interface{}) ([]*v1.PodTemplateSpec, error)

func ExtractPods

func ExtractPods(obj interface{}) ([]*v1.PodSpec, error)

func FakePodSpec

func FakePodSpec(image reference.NamedTagged) v1.PodSpec

func FakePodStatus

func FakePodStatus(image reference.NamedTagged, phase string) v1.PodStatus

func FindImageNamedTaggedMatching added in v0.2.0

func FindImageNamedTaggedMatching(pod v1.PodSpec, ref reference.Named) (reference.NamedTagged, error)

func FindImageRefMatching added in v0.2.0

func FindImageRefMatching(pod v1.PodSpec, ref reference.Named) (reference.Named, error)

func IsContainerExited

func IsContainerExited(pod v1.PodStatus, container v1.ContainerStatus) bool

If true, this means the container is gone and will never recover.

func IsUnschedulable

func IsUnschedulable(pod v1.PodStatus) (bool, string)

Returns the error message if the pod is unschedulable

func NewK8sOnlyManifest added in v0.6.0

func NewK8sOnlyManifest(name model.ManifestName, entities []K8sEntity) (model.Manifest, error)

func NewK8sOnlyManifestForTesting added in v0.6.0

func NewK8sOnlyManifestForTesting(name model.ManifestName, yaml string) model.Manifest

func NewTarget added in v0.6.0

func NewTarget(
	name model.TargetName,
	entities []K8sEntity,
	portForwards []model.PortForward,
	extraPodSelectors []labels.Selector,
	dependencyIDs []model.TargetID) (model.K8sTarget, error)

func PodContainsRef

func PodContainsRef(pod v1.PodSpec, ref reference.Named) (bool, error)

func ProvideClientConfig added in v0.7.3

func ProvideClientConfig() clientcmd.ClientConfig

func ProvideClientSet added in v0.7.4

func ProvideClientSet(cfg *rest.Config) (*kubernetes.Clientset, error)

func ProvideKubectlRunner added in v0.7.4

func ProvideKubectlRunner(kubeContext KubeContext) kubectlRunner

func ProvideRESTConfig

func ProvideRESTConfig(clientLoader clientcmd.ClientConfig) (*rest.Config, error)

func SelectorEqual added in v0.7.1

func SelectorEqual(a, b labels.Selector) bool

func SerializeYAML

func SerializeYAML(decoded []K8sEntity) (string, error)

func ServiceURL

func ServiceURL(service *v1.Service, ip NodeIP) (*url.URL, error)

func TiltDeployLabel added in v0.7.4

func TiltDeployLabel(dID model.DeployID) model.LabelPair

func TiltRunLabel added in v0.7.4

func TiltRunLabel() model.LabelPair

func TiltRunSelector added in v0.7.4

func TiltRunSelector() labels.Selector

func WaitForContainerReady

func WaitForContainerReady(ctx context.Context, client Client, pod *v1.Pod, ref reference.Named) (v1.ContainerStatus, error)

Types

type BufferCloser

type BufferCloser struct {
	*bytes.Buffer
}

func (BufferCloser) Close

func (b BufferCloser) Close() error

type Client

type Client interface {
	// Updates the entities, creating them if necessary.
	//
	// Tries to update them in-place if possible. But for certain resource types,
	// we might need to fallback to deleting and re-creating them.
	Upsert(ctx context.Context, entities []K8sEntity) error

	// Deletes all given entities.
	//
	// Currently ignores any "not found" errors, because that seems like the correct
	// behavior for our use cases.
	Delete(ctx context.Context, entities []K8sEntity) error

	// Find all the pods that match the given image, namespace, and labels.
	PodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair) ([]v1.Pod, error)

	// Find all the pods matching the given parameters, stopping on timeout or
	// when we have at least one pod.
	PollForPodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair, timeout time.Duration) ([]v1.Pod, error)

	PodByID(ctx context.Context, podID PodID, n Namespace) (*v1.Pod, error)

	// Creates a channel where all changes to the pod are brodcast.
	// Takes a pod as input, to indicate the version of the pod where we start watching.
	WatchPod(ctx context.Context, pod *v1.Pod) (watch.Interface, error)

	// Streams the container logs
	ContainerLogs(ctx context.Context, podID PodID, cName container.Name, n Namespace, startTime time.Time) (io.ReadCloser, error)

	// Opens a tunnel to the specified pod+port. Returns the tunnel's local port and a function that closes the tunnel
	ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (localPort int, closer func(), err error)

	WatchPods(ctx context.Context, lps labels.Selector) (<-chan *v1.Pod, error)

	WatchServices(ctx context.Context, lps []model.LabelPair) (<-chan *v1.Service, error)

	ConnectedToCluster(ctx context.Context) error
}

func ProvideK8sClient added in v0.7.1

func ProvideK8sClient(
	ctx context.Context,
	env Env,
	pf PortForwarder,
	configNamespace Namespace,
	runner kubectlRunner,
	clientLoader clientcmd.ClientConfig) Client

type Env

type Env string

func EnvFromString

func EnvFromString(s string) Env

func ProvideEnv added in v0.7.4

func ProvideEnv(kubeContext KubeContext) Env

func (Env) IsLocalCluster

func (e Env) IsLocalCluster() bool

type FakeK8sClient

type FakeK8sClient struct {
	Yaml        string
	DeletedYaml string
	Lb          LoadBalancerSpec

	PodsWithImageResp         PodID
	PodsWithImageError        error
	PollForPodsWithImageDelay time.Duration

	LastPodQueryNamespace Namespace
	LastPodQueryImage     reference.NamedTagged

	PodLogs            BufferCloser
	ContainerLogsError error

	LastForwardPortPodID      PodID
	LastForwardPortRemotePort int

	UpsertError error
	// contains filtered or unexported fields
}

func NewFakeK8sClient

func NewFakeK8sClient() *FakeK8sClient

func (*FakeK8sClient) ConnectedToCluster added in v0.2.0

func (c *FakeK8sClient) ConnectedToCluster(ctx context.Context) error

func (*FakeK8sClient) ContainerLogs

func (c *FakeK8sClient) ContainerLogs(ctx context.Context, pID PodID, cName container.Name, n Namespace, startTime time.Time) (io.ReadCloser, error)

func (*FakeK8sClient) Delete

func (c *FakeK8sClient) Delete(ctx context.Context, entities []K8sEntity) error

func (*FakeK8sClient) EmitPod added in v0.7.1

func (c *FakeK8sClient) EmitPod(ls labels.Selector, p *v1.Pod)

func (*FakeK8sClient) ForwardPort

func (c *FakeK8sClient) ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (int, func(), error)

func (*FakeK8sClient) PodByID

func (c *FakeK8sClient) PodByID(ctx context.Context, pID PodID, n Namespace) (*v1.Pod, error)

func (*FakeK8sClient) PodsWithImage

func (c *FakeK8sClient) PodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair) ([]v1.Pod, error)

func (*FakeK8sClient) PollForPodsWithImage

func (c *FakeK8sClient) PollForPodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair, timeout time.Duration) ([]v1.Pod, error)

func (*FakeK8sClient) SetLogs added in v0.2.0

func (c *FakeK8sClient) SetLogs(logs string)

func (*FakeK8sClient) SetPodsWithImageResp

func (c *FakeK8sClient) SetPodsWithImageResp(pID PodID)

func (*FakeK8sClient) SetPollForPodsWithImageDelay

func (c *FakeK8sClient) SetPollForPodsWithImageDelay(dur time.Duration)

func (*FakeK8sClient) Upsert

func (c *FakeK8sClient) Upsert(ctx context.Context, entities []K8sEntity) error

func (*FakeK8sClient) WatchPod

func (c *FakeK8sClient) WatchPod(ctx context.Context, pod *v1.Pod) (watch.Interface, error)

func (*FakeK8sClient) WatchPods

func (c *FakeK8sClient) WatchPods(ctx context.Context, ls labels.Selector) (<-chan *v1.Pod, error)

func (*FakeK8sClient) WatchServices

func (c *FakeK8sClient) WatchServices(ctx context.Context, lps []model.LabelPair) (<-chan *v1.Service, error)

func (*FakeK8sClient) WatchedSelectors added in v0.7.1

func (c *FakeK8sClient) WatchedSelectors() []labels.Selector

type K8sClient

type K8sClient struct {
	// contains filtered or unexported fields
}

func (K8sClient) ConnectedToCluster added in v0.2.0

func (k K8sClient) ConnectedToCluster(ctx context.Context) error

func (K8sClient) ContainerLogs

func (k K8sClient) ContainerLogs(ctx context.Context, pID PodID, cName container.Name, n Namespace, startWatchTime time.Time) (io.ReadCloser, error)

func (K8sClient) Delete

func (k K8sClient) Delete(ctx context.Context, entities []K8sEntity) error

Deletes all given entities.

Currently ignores any "not found" errors, because that seems like the correct behavior for our use cases.

func (K8sClient) ForwardPort

func (k K8sClient) ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (localPort int, closer func(), err error)

func (K8sClient) PodByID

func (k K8sClient) PodByID(ctx context.Context, pID PodID, n Namespace) (*v1.Pod, error)

func (K8sClient) PodsWithImage

func (k K8sClient) PodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair) ([]v1.Pod, error)

PodsWithImage returns the ID of the pod running the given image. If too many matches, throw an error. If no matches, return nil -- nothing is wrong, we just didn't find a result.

func (K8sClient) PollForPodsWithImage

func (k K8sClient) PollForPodsWithImage(ctx context.Context, image reference.NamedTagged, n Namespace, labels []model.LabelPair, timeout time.Duration) ([]v1.Pod, error)

func (K8sClient) Upsert

func (k K8sClient) Upsert(ctx context.Context, entities []K8sEntity) error

func (K8sClient) WatchPod

func (k K8sClient) WatchPod(ctx context.Context, pod *v1.Pod) (watch.Interface, error)

func (K8sClient) WatchPods

func (kCli K8sClient) WatchPods(ctx context.Context, ls labels.Selector) (<-chan *v1.Pod, error)

func (K8sClient) WatchServices

func (kCli K8sClient) WatchServices(ctx context.Context, lps []model.LabelPair) (<-chan *v1.Service, error)

type K8sEntity

type K8sEntity struct {
	Obj  runtime.Object
	Kind *schema.GroupVersionKind
}

func Filter added in v0.1.0

func Filter(entities []K8sEntity, test func(e K8sEntity) (bool, error)) (passing, rest []K8sEntity, err error)

Filter returns two slices of entities: those passing the given test, and the remainder of the input.

func FilterByHasPodTemplateSpec added in v0.7.5

func FilterByHasPodTemplateSpec(entities []K8sEntity) (passing, rest []K8sEntity, err error)

func FilterByImage added in v0.1.0

func FilterByImage(entities []K8sEntity, img reference.Named) (passing, rest []K8sEntity, err error)

func FilterByLabels added in v0.1.0

func FilterByLabels(entities []K8sEntity, labels map[string]string) (passing, rest []K8sEntity, err error)

func FilterByMatchesPodTemplateSpec added in v0.7.5

func FilterByMatchesPodTemplateSpec(withPodSpec K8sEntity, entities []K8sEntity) (passing, rest []K8sEntity, err error)

func ImmutableEntities

func ImmutableEntities(entities []K8sEntity) []K8sEntity

func InjectImageDigest

func InjectImageDigest(entity K8sEntity, injectRef reference.Named, policy v1.PullPolicy) (K8sEntity, bool, error)

Iterate through the fields of a k8s entity and replace a image name with its digest.

policy: The pull policy to set on the replaced image.

When working with a local k8s cluster, we want to set this to Never,
to ensure that k8s fails hard if the image is missing from docker.

Returns: the new entity, whether the image was replaced, and an error.

func InjectImagePullPolicy

func InjectImagePullPolicy(entity K8sEntity, policy v1.PullPolicy) (K8sEntity, error)

Iterate through the fields of a k8s entity and replace the image pull policy on all images.

func InjectLabels

func InjectLabels(entity K8sEntity, labels []model.LabelPair) (K8sEntity, error)

func MutableEntities

func MutableEntities(entities []K8sEntity) []K8sEntity

func OverwriteLabels added in v0.7.5

func OverwriteLabels(entity K8sEntity, labels []model.LabelPair) (K8sEntity, error)

func ParseYAMLFromString

func ParseYAMLFromString(yaml string) ([]K8sEntity, error)

func (K8sEntity) DeepCopy

func (e K8sEntity) DeepCopy() K8sEntity

func (K8sEntity) FindImages added in v0.4.1

func (e K8sEntity) FindImages() ([]reference.Named, error)

func (K8sEntity) HasImage added in v0.1.0

func (e K8sEntity) HasImage(image reference.Named) (bool, error)

HasImage indicates whether the given entity is tagged with the given image.

func (K8sEntity) ImmutableOnceCreated

func (e K8sEntity) ImmutableOnceCreated() bool

Most entities can be updated once running, but a few cannot.

func (K8sEntity) MatchesLabels added in v0.1.0

func (e K8sEntity) MatchesLabels(labels map[string]string) bool

MatchesLevel indicates whether the selector of the given entity matches the given label(s). Currently only supports Services, but may be expanded to support other types that match pods via selectors.

func (K8sEntity) Name

func (e K8sEntity) Name() string

func (K8sEntity) Namespace

func (e K8sEntity) Namespace() Namespace

func (K8sEntity) ResourceName added in v0.7.5

func (e K8sEntity) ResourceName() string

type KubeContext added in v0.6.0

type KubeContext string

func ProvideKubeContext added in v0.7.4

func ProvideKubeContext(clientLoader clientcmd.ClientConfig) (KubeContext, error)

type LoadBalancer

type LoadBalancer struct {
	Spec LoadBalancerSpec
	URL  *url.URL
}

type LoadBalancerSpec

type LoadBalancerSpec struct {
	Name      string
	Namespace Namespace
	Ports     []int32
}

func ToLoadBalancerSpec

func ToLoadBalancerSpec(entity K8sEntity) (LoadBalancerSpec, bool)

Try to convert the current entity to a LoadBalancerSpec service

func ToLoadBalancerSpecs

func ToLoadBalancerSpecs(entities []K8sEntity) []LoadBalancerSpec

type Namespace

type Namespace string

func NamespaceFromPod

func NamespaceFromPod(pod *v1.Pod) Namespace

func ProvideConfigNamespace added in v0.7.3

func ProvideConfigNamespace(clientLoader clientcmd.ClientConfig) Namespace

The namespace in the kubeconfig. Used as a default namespace in some (but not all) client commands. https://godoc.org/k8s.io/client-go/tools/clientcmd/api/v1#Context

func (Namespace) String

func (n Namespace) String() string

type NodeID

type NodeID string

func NodeIDFromPod

func NodeIDFromPod(pod *v1.Pod) NodeID

func (NodeID) String

func (nID NodeID) String() string

type NodeIP added in v0.1.0

type NodeIP string

Some K8s environments expose a single IP for the whole cluster.

func DetectNodeIP added in v0.1.0

func DetectNodeIP(ctx context.Context, env Env) (NodeIP, error)

type PodID

type PodID string

func PodIDFromPod

func PodIDFromPod(pod *v1.Pod) PodID

func (PodID) Empty

func (pID PodID) Empty() bool

func (PodID) String

func (pID PodID) String() string

type PortForwarder

type PortForwarder func(ctx context.Context, restConfig *rest.Config, core apiv1.CoreV1Interface, namespace string, podID PodID, localPort int, remotePort int) (closer func(), err error)

func ProvidePortForwarder

func ProvidePortForwarder() PortForwarder

type ServiceName

type ServiceName string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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