proxyclass.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !plan9
  4. package main
  5. import (
  6. "context"
  7. "fmt"
  8. "slices"
  9. "strings"
  10. "sync"
  11. dockerref "github.com/distribution/reference"
  12. "go.uber.org/zap"
  13. corev1 "k8s.io/api/core/v1"
  14. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  15. apiequality "k8s.io/apimachinery/pkg/api/equality"
  16. apierrors "k8s.io/apimachinery/pkg/api/errors"
  17. apivalidation "k8s.io/apimachinery/pkg/api/validation"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. metavalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/apimachinery/pkg/util/validation/field"
  22. "k8s.io/client-go/tools/record"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. "sigs.k8s.io/controller-runtime/pkg/reconcile"
  25. tsoperator "tailscale.com/k8s-operator"
  26. tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
  27. "tailscale.com/tstime"
  28. "tailscale.com/util/clientmetric"
  29. "tailscale.com/util/set"
  30. )
  31. const (
  32. reasonProxyClassInvalid = "ProxyClassInvalid"
  33. reasonProxyClassValid = "ProxyClassValid"
  34. reasonCustomTSEnvVar = "CustomTSEnvVar"
  35. messageProxyClassInvalid = "ProxyClass is not valid: %v"
  36. messageCustomTSEnvVar = "ProxyClass overrides the default value for %s env var for %s container. Running with custom values for Tailscale env vars is not recommended and might break in the future."
  37. )
  38. type ProxyClassReconciler struct {
  39. client.Client
  40. recorder record.EventRecorder
  41. logger *zap.SugaredLogger
  42. clock tstime.Clock
  43. tsNamespace string
  44. mu sync.Mutex // protects following
  45. // managedProxyClasses is a set of all ProxyClass resources that we're currently
  46. // managing. This is only used for metrics.
  47. managedProxyClasses set.Slice[types.UID]
  48. // nodePortRange is the NodePort range set for the Kubernetes Cluster. This is used
  49. // when validating port ranges configured by users for spec.StaticEndpoints
  50. nodePortRange *tsapi.PortRange
  51. }
  52. // gaugeProxyClassResources tracks the number of ProxyClass resources
  53. // that we're currently managing.
  54. var gaugeProxyClassResources = clientmetric.NewGauge("k8s_proxyclass_resources")
  55. func (pcr *ProxyClassReconciler) Reconcile(ctx context.Context, req reconcile.Request) (res reconcile.Result, err error) {
  56. logger := pcr.logger.With("ProxyClass", req.Name)
  57. logger.Debugf("starting reconcile")
  58. defer logger.Debugf("reconcile finished")
  59. pc := new(tsapi.ProxyClass)
  60. err = pcr.Get(ctx, req.NamespacedName, pc)
  61. if apierrors.IsNotFound(err) {
  62. logger.Debugf("ProxyClass not found, assuming it was deleted")
  63. return reconcile.Result{}, nil
  64. } else if err != nil {
  65. return reconcile.Result{}, fmt.Errorf("failed to get tailscale.com ProxyClass: %w", err)
  66. }
  67. if !pc.DeletionTimestamp.IsZero() {
  68. logger.Debugf("ProxyClass is being deleted")
  69. return reconcile.Result{}, pcr.maybeCleanup(ctx, logger, pc)
  70. }
  71. // Add a finalizer so that we can ensure that metrics get updated when
  72. // this ProxyClass is deleted.
  73. if !slices.Contains(pc.Finalizers, FinalizerName) {
  74. logger.Debugf("updating ProxyClass finalizers")
  75. pc.Finalizers = append(pc.Finalizers, FinalizerName)
  76. if err := pcr.Update(ctx, pc); err != nil {
  77. return res, fmt.Errorf("failed to add finalizer: %w", err)
  78. }
  79. }
  80. // Ensure this ProxyClass is tracked in metrics.
  81. pcr.mu.Lock()
  82. pcr.managedProxyClasses.Add(pc.UID)
  83. gaugeProxyClassResources.Set(int64(pcr.managedProxyClasses.Len()))
  84. pcr.mu.Unlock()
  85. oldPCStatus := pc.Status.DeepCopy()
  86. if errs := pcr.validate(ctx, pc, logger); errs != nil {
  87. msg := fmt.Sprintf(messageProxyClassInvalid, errs.ToAggregate().Error())
  88. pcr.recorder.Event(pc, corev1.EventTypeWarning, reasonProxyClassInvalid, msg)
  89. tsoperator.SetProxyClassCondition(pc, tsapi.ProxyClassReady, metav1.ConditionFalse, reasonProxyClassInvalid, msg, pc.Generation, pcr.clock, logger)
  90. } else {
  91. tsoperator.SetProxyClassCondition(pc, tsapi.ProxyClassReady, metav1.ConditionTrue, reasonProxyClassValid, reasonProxyClassValid, pc.Generation, pcr.clock, logger)
  92. }
  93. if !apiequality.Semantic.DeepEqual(oldPCStatus, &pc.Status) {
  94. if err := pcr.Client.Status().Update(ctx, pc); err != nil {
  95. logger.Errorf("error updating ProxyClass status: %v", err)
  96. return reconcile.Result{}, err
  97. }
  98. }
  99. return reconcile.Result{}, nil
  100. }
  101. func (pcr *ProxyClassReconciler) validate(ctx context.Context, pc *tsapi.ProxyClass, logger *zap.SugaredLogger) (violations field.ErrorList) {
  102. if sts := pc.Spec.StatefulSet; sts != nil {
  103. if len(sts.Labels) > 0 {
  104. if errs := metavalidation.ValidateLabels(sts.Labels.Parse(), field.NewPath(".spec.statefulSet.labels")); errs != nil {
  105. violations = append(violations, errs...)
  106. }
  107. }
  108. if len(sts.Annotations) > 0 {
  109. if errs := apivalidation.ValidateAnnotations(sts.Annotations, field.NewPath(".spec.statefulSet.annotations")); errs != nil {
  110. violations = append(violations, errs...)
  111. }
  112. }
  113. if pod := sts.Pod; pod != nil {
  114. if len(pod.Labels) > 0 {
  115. if errs := metavalidation.ValidateLabels(pod.Labels.Parse(), field.NewPath(".spec.statefulSet.pod.labels")); errs != nil {
  116. violations = append(violations, errs...)
  117. }
  118. }
  119. if len(pod.Annotations) > 0 {
  120. if errs := apivalidation.ValidateAnnotations(pod.Annotations, field.NewPath(".spec.statefulSet.pod.annotations")); errs != nil {
  121. violations = append(violations, errs...)
  122. }
  123. }
  124. if tc := pod.TailscaleContainer; tc != nil {
  125. for _, e := range tc.Env {
  126. if strings.HasPrefix(string(e.Name), "TS_") {
  127. pcr.recorder.Event(pc, corev1.EventTypeWarning, reasonCustomTSEnvVar, fmt.Sprintf(messageCustomTSEnvVar, string(e.Name), "tailscale"))
  128. }
  129. if strings.EqualFold(string(e.Name), "EXPERIMENTAL_TS_CONFIGFILE_PATH") {
  130. pcr.recorder.Event(pc, corev1.EventTypeWarning, reasonCustomTSEnvVar, fmt.Sprintf(messageCustomTSEnvVar, string(e.Name), "tailscale"))
  131. }
  132. if strings.EqualFold(string(e.Name), "EXPERIMENTAL_ALLOW_PROXYING_CLUSTER_TRAFFIC_VIA_INGRESS") {
  133. pcr.recorder.Event(pc, corev1.EventTypeWarning, reasonCustomTSEnvVar, fmt.Sprintf(messageCustomTSEnvVar, string(e.Name), "tailscale"))
  134. }
  135. }
  136. if tc.Image != "" {
  137. // Same validation as used by kubelet https://github.com/kubernetes/kubernetes/blob/release-1.30/pkg/kubelet/images/image_manager.go#L212
  138. if _, err := dockerref.ParseNormalizedNamed(tc.Image); err != nil {
  139. violations = append(violations, field.TypeInvalid(field.NewPath("spec", "statefulSet", "pod", "tailscaleContainer", "image"), tc.Image, err.Error()))
  140. }
  141. }
  142. }
  143. if tc := pod.TailscaleInitContainer; tc != nil {
  144. if tc.Image != "" {
  145. // Same validation as used by kubelet https://github.com/kubernetes/kubernetes/blob/release-1.30/pkg/kubelet/images/image_manager.go#L212
  146. if _, err := dockerref.ParseNormalizedNamed(tc.Image); err != nil {
  147. violations = append(violations, field.TypeInvalid(field.NewPath("spec", "statefulSet", "pod", "tailscaleInitContainer", "image"), tc.Image, err.Error()))
  148. }
  149. }
  150. if tc.Debug != nil {
  151. violations = append(violations, field.TypeInvalid(field.NewPath("spec", "statefulSet", "pod", "tailscaleInitContainer", "debug"), tc.Debug, "debug settings cannot be configured on the init container"))
  152. }
  153. }
  154. }
  155. }
  156. if pc.Spec.Metrics != nil && pc.Spec.Metrics.ServiceMonitor != nil && pc.Spec.Metrics.ServiceMonitor.Enable {
  157. found, err := hasServiceMonitorCRD(ctx, pcr.Client)
  158. if err != nil {
  159. pcr.logger.Infof("[unexpected]: error retrieving %q CRD: %v", serviceMonitorCRD, err)
  160. // best effort validation - don't error out here
  161. } else if !found {
  162. msg := fmt.Sprintf("ProxyClass defines that a ServiceMonitor custom resource should be created, but %q CRD was not found", serviceMonitorCRD)
  163. violations = append(violations, field.TypeInvalid(field.NewPath("spec", "metrics", "serviceMonitor"), "enable", msg))
  164. }
  165. }
  166. if pc.Spec.Metrics != nil && pc.Spec.Metrics.ServiceMonitor != nil && len(pc.Spec.Metrics.ServiceMonitor.Labels) > 0 {
  167. if errs := metavalidation.ValidateLabels(pc.Spec.Metrics.ServiceMonitor.Labels.Parse(), field.NewPath(".spec.metrics.serviceMonitor.labels")); errs != nil {
  168. violations = append(violations, errs...)
  169. }
  170. }
  171. if stat := pc.Spec.StaticEndpoints; stat != nil {
  172. if err := validateNodePortRanges(ctx, pcr.Client, pcr.nodePortRange, pc); err != nil {
  173. var prs tsapi.PortRanges = stat.NodePort.Ports
  174. violations = append(violations, field.TypeInvalid(field.NewPath("spec", "staticEndpoints", "nodePort", "ports"), prs.String(), err.Error()))
  175. }
  176. if len(stat.NodePort.Selector) < 1 {
  177. logger.Debug("no Selectors specified on `spec.staticEndpoints.nodePort.selectors` field")
  178. }
  179. }
  180. // We do not validate embedded fields (security context, resource
  181. // requirements etc) as we inherit upstream validation for those fields.
  182. // Invalid values would get rejected by upstream validations at apply
  183. // time.
  184. return violations
  185. }
  186. func hasServiceMonitorCRD(ctx context.Context, cl client.Client) (bool, error) {
  187. sm := &apiextensionsv1.CustomResourceDefinition{}
  188. if err := cl.Get(ctx, types.NamespacedName{Name: serviceMonitorCRD}, sm); apierrors.IsNotFound(err) {
  189. return false, nil
  190. } else if err != nil {
  191. return false, err
  192. }
  193. return true, nil
  194. }
  195. // maybeCleanup removes tailscale.com finalizer and ensures that the ProxyClass
  196. // is no longer counted towards k8s_proxyclass_resources.
  197. func (pcr *ProxyClassReconciler) maybeCleanup(ctx context.Context, logger *zap.SugaredLogger, pc *tsapi.ProxyClass) error {
  198. ix := slices.Index(pc.Finalizers, FinalizerName)
  199. if ix < 0 {
  200. logger.Debugf("no finalizer, nothing to do")
  201. pcr.mu.Lock()
  202. defer pcr.mu.Unlock()
  203. pcr.managedProxyClasses.Remove(pc.UID)
  204. gaugeProxyClassResources.Set(int64(pcr.managedProxyClasses.Len()))
  205. return nil
  206. }
  207. pc.Finalizers = append(pc.Finalizers[:ix], pc.Finalizers[ix+1:]...)
  208. if err := pcr.Update(ctx, pc); err != nil {
  209. return fmt.Errorf("failed to remove finalizer: %w", err)
  210. }
  211. pcr.mu.Lock()
  212. defer pcr.mu.Unlock()
  213. pcr.managedProxyClasses.Remove(pc.UID)
  214. gaugeProxyClassResources.Set(int64(pcr.managedProxyClasses.Len()))
  215. logger.Infof("ProxyClass resources have been cleaned up")
  216. return nil
  217. }