proxyclass_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !plan9
  4. // tailscale-operator provides a way to expose services running in a Kubernetes
  5. // cluster to your Tailnet.
  6. package main
  7. import (
  8. "testing"
  9. "time"
  10. "go.uber.org/zap"
  11. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  12. "k8s.io/apimachinery/pkg/types"
  13. "k8s.io/client-go/tools/record"
  14. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  15. tsoperator "tailscale.com/k8s-operator"
  16. tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
  17. "tailscale.com/tstest"
  18. )
  19. func TestProxyClass(t *testing.T) {
  20. pc := &tsapi.ProxyClass{
  21. TypeMeta: metav1.TypeMeta{Kind: "ProxyClass", APIVersion: "tailscale.com/v1alpha1"},
  22. ObjectMeta: metav1.ObjectMeta{
  23. Name: "test",
  24. // The apiserver is supposed to set the UID, but the fake client
  25. // doesn't. So, set it explicitly because other code later depends
  26. // on it being set.
  27. UID: types.UID("1234-UID"),
  28. },
  29. Spec: tsapi.ProxyClassSpec{
  30. StatefulSet: &tsapi.StatefulSet{
  31. Labels: map[string]string{"foo": "bar", "xyz1234": "abc567"},
  32. Annotations: map[string]string{"foo.io/bar": "{'key': 'val1232'}"},
  33. Pod: &tsapi.Pod{
  34. Labels: map[string]string{"foo": "bar", "xyz1234": "abc567"},
  35. Annotations: map[string]string{"foo.io/bar": "{'key': 'val1232'}"},
  36. TailscaleContainer: &tsapi.Container{
  37. Env: []tsapi.Env{{Name: "FOO", Value: "BAR"}},
  38. ImagePullPolicy: "IfNotPresent",
  39. Image: "ghcr.my-repo/tailscale:v0.01testsomething",
  40. },
  41. },
  42. },
  43. },
  44. }
  45. fc := fake.NewClientBuilder().
  46. WithScheme(tsapi.GlobalScheme).
  47. WithObjects(pc).
  48. WithStatusSubresource(pc).
  49. Build()
  50. zl, err := zap.NewDevelopment()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. fr := record.NewFakeRecorder(3) // bump this if you expect a test case to throw more events
  55. cl := tstest.NewClock(tstest.ClockOpts{})
  56. pcr := &ProxyClassReconciler{
  57. Client: fc,
  58. logger: zl.Sugar(),
  59. clock: cl,
  60. recorder: fr,
  61. }
  62. // 1. A valid ProxyClass resource gets its status updated to Ready.
  63. expectReconciled(t, pcr, "", "test")
  64. pc.Status.Conditions = append(pc.Status.Conditions, tsapi.ConnectorCondition{
  65. Type: tsapi.ProxyClassready,
  66. Status: metav1.ConditionTrue,
  67. Reason: reasonProxyClassValid,
  68. Message: reasonProxyClassValid,
  69. LastTransitionTime: &metav1.Time{Time: cl.Now().Truncate(time.Second)},
  70. })
  71. expectEqual(t, fc, pc, nil)
  72. // 2. A ProxyClass resource with invalid labels gets its status updated to Invalid with an error message.
  73. pc.Spec.StatefulSet.Labels["foo"] = "?!someVal"
  74. mustUpdate(t, fc, "", "test", func(proxyClass *tsapi.ProxyClass) {
  75. proxyClass.Spec.StatefulSet.Labels = pc.Spec.StatefulSet.Labels
  76. })
  77. expectReconciled(t, pcr, "", "test")
  78. msg := `ProxyClass is not valid: .spec.statefulSet.labels: Invalid value: "?!someVal": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')`
  79. tsoperator.SetProxyClassCondition(pc, tsapi.ProxyClassready, metav1.ConditionFalse, reasonProxyClassInvalid, msg, 0, cl, zl.Sugar())
  80. expectEqual(t, fc, pc, nil)
  81. expectedEvent := "Warning ProxyClassInvalid ProxyClass is not valid: .spec.statefulSet.labels: Invalid value: \"?!someVal\": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')"
  82. expectEvents(t, fr, []string{expectedEvent})
  83. // 3. A ProxyClass resource with invalid image reference gets it status updated to Invalid with an error message.
  84. pc.Spec.StatefulSet.Labels = nil
  85. pc.Spec.StatefulSet.Pod.TailscaleContainer.Image = "FOO bar"
  86. mustUpdate(t, fc, "", "test", func(proxyClass *tsapi.ProxyClass) {
  87. proxyClass.Spec.StatefulSet.Labels = nil
  88. proxyClass.Spec.StatefulSet.Pod.TailscaleContainer.Image = pc.Spec.StatefulSet.Pod.TailscaleContainer.Image
  89. })
  90. expectReconciled(t, pcr, "", "test")
  91. msg = `ProxyClass is not valid: spec.statefulSet.pod.tailscaleContainer.image: Invalid value: "FOO bar": invalid reference format: repository name (library/FOO bar) must be lowercase`
  92. tsoperator.SetProxyClassCondition(pc, tsapi.ProxyClassready, metav1.ConditionFalse, reasonProxyClassInvalid, msg, 0, cl, zl.Sugar())
  93. expectEqual(t, fc, pc, nil)
  94. expectedEvent = `Warning ProxyClassInvalid ProxyClass is not valid: spec.statefulSet.pod.tailscaleContainer.image: Invalid value: "FOO bar": invalid reference format: repository name (library/FOO bar) must be lowercase`
  95. expectEvents(t, fr, []string{expectedEvent})
  96. // 4. A ProxyClass resource with invalid init container image reference gets it status updated to Invalid with an error message.
  97. pc.Spec.StatefulSet.Labels = nil
  98. pc.Spec.StatefulSet.Pod.TailscaleContainer.Image = ""
  99. pc.Spec.StatefulSet.Pod.TailscaleInitContainer = &tsapi.Container{
  100. Image: "FOO bar",
  101. }
  102. mustUpdate(t, fc, "", "test", func(proxyClass *tsapi.ProxyClass) {
  103. proxyClass.Spec.StatefulSet.Pod.TailscaleContainer.Image = pc.Spec.StatefulSet.Pod.TailscaleContainer.Image
  104. proxyClass.Spec.StatefulSet.Pod.TailscaleInitContainer = &tsapi.Container{
  105. Image: pc.Spec.StatefulSet.Pod.TailscaleInitContainer.Image,
  106. }
  107. })
  108. expectReconciled(t, pcr, "", "test")
  109. msg = `ProxyClass is not valid: spec.statefulSet.pod.tailscaleInitContainer.image: Invalid value: "FOO bar": invalid reference format: repository name (library/FOO bar) must be lowercase`
  110. tsoperator.SetProxyClassCondition(pc, tsapi.ProxyClassready, metav1.ConditionFalse, reasonProxyClassInvalid, msg, 0, cl, zl.Sugar())
  111. expectEqual(t, fc, pc, nil)
  112. expectedEvent = `Warning ProxyClassInvalid ProxyClass is not valid: spec.statefulSet.pod.tailscaleInitContainer.image: Invalid value: "FOO bar": invalid reference format: repository name (library/FOO bar) must be lowercase`
  113. expectEvents(t, fr, []string{expectedEvent})
  114. // 5. An valid ProxyClass but with a Tailscale env vars set results in warning events.
  115. pc.Spec.StatefulSet.Pod.TailscaleInitContainer.Image = "" // unset previous test
  116. mustUpdate(t, fc, "", "test", func(proxyClass *tsapi.ProxyClass) {
  117. proxyClass.Spec.StatefulSet.Pod.TailscaleInitContainer.Image = pc.Spec.StatefulSet.Pod.TailscaleInitContainer.Image
  118. proxyClass.Spec.StatefulSet.Pod.TailscaleContainer.Env = []tsapi.Env{{Name: "TS_USERSPACE", Value: "true"}, {Name: "EXPERIMENTAL_TS_CONFIGFILE_PATH"}, {Name: "EXPERIMENTAL_ALLOW_PROXYING_CLUSTER_TRAFFIC_VIA_INGRESS"}}
  119. })
  120. expectedEvents := []string{"Warning CustomTSEnvVar ProxyClass overrides the default value for TS_USERSPACE env var for tailscale container. Running with custom values for Tailscale env vars is not recommended and might break in the future.",
  121. "Warning CustomTSEnvVar ProxyClass overrides the default value for EXPERIMENTAL_TS_CONFIGFILE_PATH env var for tailscale container. Running with custom values for Tailscale env vars is not recommended and might break in the future.",
  122. "Warning CustomTSEnvVar ProxyClass overrides the default value for EXPERIMENTAL_ALLOW_PROXYING_CLUSTER_TRAFFIC_VIA_INGRESS env var for tailscale container. Running with custom values for Tailscale env vars is not recommended and might break in the future."}
  123. expectReconciled(t, pcr, "", "test")
  124. expectEvents(t, fr, expectedEvents)
  125. }