2023-12-14 13:51:59 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build !plan9
|
|
|
|
|
|
|
|
package kube
|
|
|
|
|
|
|
|
import (
|
|
|
|
"slices"
|
2024-02-13 05:27:54 +00:00
|
|
|
"time"
|
2023-12-14 13:51:59 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
xslices "golang.org/x/exp/slices"
|
2024-06-18 18:01:40 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-12-14 13:51:59 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
|
|
|
|
"tailscale.com/tstime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetConnectorCondition ensures that Connector status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
2024-02-13 05:27:54 +00:00
|
|
|
// changes.
|
2024-06-18 18:01:40 +00:00
|
|
|
func SetConnectorCondition(cn *tsapi.Connector, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) {
|
2024-02-13 05:27:54 +00:00
|
|
|
conds := updateCondition(cn.Status.Conditions, conditionType, status, reason, message, gen, clock, logger)
|
|
|
|
cn.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:18:23 +00:00
|
|
|
// RemoveConnectorCondition will remove condition of the given type if it exists.
|
2024-06-18 18:01:40 +00:00
|
|
|
func RemoveConnectorCondition(conn *tsapi.Connector, conditionType tsapi.ConditionType) {
|
|
|
|
conn.Status.Conditions = slices.DeleteFunc(conn.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(conditionType)
|
2024-02-13 05:27:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetProxyClassCondition ensures that ProxyClass status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
|
|
|
// changes.
|
2024-06-18 18:01:40 +00:00
|
|
|
func SetProxyClassCondition(pc *tsapi.ProxyClass, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) {
|
2024-02-13 05:27:54 +00:00
|
|
|
conds := updateCondition(pc.Status.Conditions, conditionType, status, reason, message, gen, clock, logger)
|
|
|
|
pc.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:18:23 +00:00
|
|
|
// SetDNSConfigCondition ensures that DNSConfig status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
|
|
|
// changes
|
2024-06-18 18:01:40 +00:00
|
|
|
func SetDNSConfigCondition(dnsCfg *tsapi.DNSConfig, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) {
|
2024-04-30 19:18:23 +00:00
|
|
|
conds := updateCondition(dnsCfg.Status.Conditions, conditionType, status, reason, message, gen, clock, logger)
|
|
|
|
dnsCfg.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:01:40 +00:00
|
|
|
// SetServiceCondition ensures that Service status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
|
|
|
// changes.
|
|
|
|
func SetServiceCondition(svc *corev1.Service, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, clock tstime.Clock, logger *zap.SugaredLogger) {
|
|
|
|
conds := updateCondition(svc.Status.Conditions, conditionType, status, reason, message, 0, clock, logger)
|
|
|
|
svc.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-10-04 12:11:35 +00:00
|
|
|
// GetServiceCondition returns Service condition with the specified type, if it exists on the Service.
|
|
|
|
func GetServiceCondition(svc *corev1.Service, conditionType tsapi.ConditionType) *metav1.Condition {
|
|
|
|
idx := xslices.IndexFunc(svc.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(conditionType)
|
|
|
|
})
|
|
|
|
|
|
|
|
if idx == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &svc.Status.Conditions[idx]
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:01:40 +00:00
|
|
|
// RemoveServiceCondition will remove condition of the given type if it exists.
|
|
|
|
func RemoveServiceCondition(svc *corev1.Service, conditionType tsapi.ConditionType) {
|
|
|
|
svc.Status.Conditions = slices.DeleteFunc(svc.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(conditionType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-04 12:11:35 +00:00
|
|
|
func EgressServiceIsValidAndConfigured(svc *corev1.Service) bool {
|
|
|
|
for _, typ := range []tsapi.ConditionType{tsapi.EgressSvcValid, tsapi.EgressSvcConfigured} {
|
|
|
|
cond := GetServiceCondition(svc, typ)
|
|
|
|
if cond == nil || cond.Status != metav1.ConditionTrue {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-09-11 11:19:29 +00:00
|
|
|
// SetRecorderCondition ensures that Recorder status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
|
|
|
// changes.
|
|
|
|
func SetRecorderCondition(tsr *tsapi.Recorder, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) {
|
|
|
|
conds := updateCondition(tsr.Status.Conditions, conditionType, status, reason, message, gen, clock, logger)
|
|
|
|
tsr.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-10-07 13:58:45 +00:00
|
|
|
// SetProxyGroupCondition ensures that ProxyGroup status has a condition with the
|
|
|
|
// given attributes. LastTransitionTime gets set every time condition's status
|
|
|
|
// changes.
|
|
|
|
func SetProxyGroupCondition(pg *tsapi.ProxyGroup, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) {
|
|
|
|
conds := updateCondition(pg.Status.Conditions, conditionType, status, reason, message, gen, clock, logger)
|
|
|
|
pg.Status.Conditions = conds
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:01:40 +00:00
|
|
|
func updateCondition(conds []metav1.Condition, conditionType tsapi.ConditionType, status metav1.ConditionStatus, reason, message string, gen int64, clock tstime.Clock, logger *zap.SugaredLogger) []metav1.Condition {
|
|
|
|
newCondition := metav1.Condition{
|
|
|
|
Type: string(conditionType),
|
2023-12-14 13:51:59 +00:00
|
|
|
Status: status,
|
|
|
|
Reason: reason,
|
|
|
|
Message: message,
|
|
|
|
ObservedGeneration: gen,
|
|
|
|
}
|
|
|
|
|
2024-02-13 05:27:54 +00:00
|
|
|
nowTime := metav1.NewTime(clock.Now().Truncate(time.Second))
|
2024-06-18 18:01:40 +00:00
|
|
|
newCondition.LastTransitionTime = nowTime
|
2023-12-14 13:51:59 +00:00
|
|
|
|
2024-06-18 18:01:40 +00:00
|
|
|
idx := xslices.IndexFunc(conds, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(conditionType)
|
2023-12-14 13:51:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if idx == -1 {
|
2024-02-13 05:27:54 +00:00
|
|
|
conds = append(conds, newCondition)
|
|
|
|
return conds
|
2023-12-14 13:51:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 05:27:54 +00:00
|
|
|
cond := conds[idx] // update the existing condition
|
2024-04-30 19:18:23 +00:00
|
|
|
|
|
|
|
// If this update doesn't contain a state transition, don't update last
|
|
|
|
// transition time.
|
2023-12-14 13:51:59 +00:00
|
|
|
if cond.Status == status {
|
|
|
|
newCondition.LastTransitionTime = cond.LastTransitionTime
|
|
|
|
} else {
|
2024-02-13 05:27:54 +00:00
|
|
|
logger.Infof("Status change for condition %s from %s to %s", conditionType, cond.Status, status)
|
2023-12-14 13:51:59 +00:00
|
|
|
}
|
2024-02-13 05:27:54 +00:00
|
|
|
conds[idx] = newCondition
|
|
|
|
return conds
|
2023-12-14 13:51:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 05:27:54 +00:00
|
|
|
func ProxyClassIsReady(pc *tsapi.ProxyClass) bool {
|
2024-06-18 18:01:40 +00:00
|
|
|
idx := xslices.IndexFunc(pc.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(tsapi.ProxyClassready)
|
2023-12-14 13:51:59 +00:00
|
|
|
})
|
2024-02-13 05:27:54 +00:00
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cond := pc.Status.Conditions[idx]
|
|
|
|
return cond.Status == metav1.ConditionTrue && cond.ObservedGeneration == pc.Generation
|
2023-12-14 13:51:59 +00:00
|
|
|
}
|
2024-04-30 19:18:23 +00:00
|
|
|
|
2024-10-04 12:11:35 +00:00
|
|
|
func ProxyGroupIsReady(pg *tsapi.ProxyGroup) bool {
|
|
|
|
idx := xslices.IndexFunc(pg.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(tsapi.ProxyGroupReady)
|
|
|
|
})
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cond := pg.Status.Conditions[idx]
|
|
|
|
return cond.Status == metav1.ConditionTrue && cond.ObservedGeneration == pg.Generation
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:18:23 +00:00
|
|
|
func DNSCfgIsReady(cfg *tsapi.DNSConfig) bool {
|
2024-06-18 18:01:40 +00:00
|
|
|
idx := xslices.IndexFunc(cfg.Status.Conditions, func(cond metav1.Condition) bool {
|
|
|
|
return cond.Type == string(tsapi.NameserverReady)
|
2024-04-30 19:18:23 +00:00
|
|
|
})
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cond := cfg.Status.Conditions[idx]
|
|
|
|
return cond.Status == metav1.ConditionTrue && cond.ObservedGeneration == cfg.Generation
|
|
|
|
}
|