|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "net/http" |
| 25 | + "strconv" |
| 26 | + "sync/atomic" |
| 27 | + "time" |
| 28 | + |
| 29 | + "k8s.io/klog/v2" |
| 30 | + |
| 31 | + "k8s.io/kops/pkg/wellknownports" |
| 32 | +) |
| 33 | + |
| 34 | +type applyChannelReadiness struct { |
| 35 | + ready atomic.Bool |
| 36 | + addr string // resolved listen address; read only by tests (which bind :0) |
| 37 | +} |
| 38 | + |
| 39 | +func (r *applyChannelReadiness) recordApplyResult(err error) { |
| 40 | + r.ready.Store(err == nil) |
| 41 | +} |
| 42 | + |
| 43 | +// serveReadiness serves /readyz on loopback for the kubelet readiness probe until ctx is cancelled: |
| 44 | +// 200 when ready is true, 503 otherwise. The pod runs with hostNetwork, so the kubelet reaches it |
| 45 | +// via 127.0.0.1 in the host network namespace. |
| 46 | +func serveReadiness(ctx context.Context) (*applyChannelReadiness, error) { |
| 47 | + addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(wellknownports.KopsChannelsHealthCheck)) |
| 48 | + return serveReadinessOnAddr(ctx, addr) |
| 49 | +} |
| 50 | + |
| 51 | +func serveReadinessOnAddr(ctx context.Context, addr string) (*applyChannelReadiness, error) { |
| 52 | + readiness := &applyChannelReadiness{} |
| 53 | + |
| 54 | + mux := http.NewServeMux() |
| 55 | + mux.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) { |
| 56 | + if readiness.ready.Load() { |
| 57 | + w.WriteHeader(http.StatusOK) |
| 58 | + _, _ = w.Write([]byte("ok\n")) |
| 59 | + } else { |
| 60 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 61 | + _, _ = w.Write([]byte("apply iterations are failing\n")) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + listener, err := net.Listen("tcp", addr) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("listening on %s: %w", addr, err) |
| 68 | + } |
| 69 | + readiness.addr = listener.Addr().String() |
| 70 | + |
| 71 | + server := &http.Server{ |
| 72 | + Handler: mux, |
| 73 | + ReadHeaderTimeout: 5 * time.Second, |
| 74 | + } |
| 75 | + |
| 76 | + go func() { |
| 77 | + <-ctx.Done() |
| 78 | + _ = server.Close() |
| 79 | + }() |
| 80 | + go func() { |
| 81 | + if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 82 | + klog.Fatalf("kops-channels readiness server stopped: %v", err) |
| 83 | + } |
| 84 | + }() |
| 85 | + return readiness, nil |
| 86 | +} |
0 commit comments