Skip to content

Commit ae1ad29

Browse files
committed
some improvements
1 parent 09f0a0f commit ae1ad29

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

cmd/app/app.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ func NewCommand(ctx context.Context) *cobra.Command {
131131
opts.KubeInterval,
132132
opts.KubeChannel,
133133
)
134-
if err := mgr.Add(kubeController); err != nil {
135-
return err
134+
135+
// Only add to manager if controller was created (channel was specified)
136+
if kubeController != nil {
137+
if err := mgr.Add(kubeController); err != nil {
138+
return err
139+
}
140+
log.WithField("channel", opts.KubeChannel).Info("Kubernetes version checking enabled")
136141
}
137142

138143
// Start the manager and all controllers

docs/new_features.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,34 @@ version_checker_is_latest_kube_version{current_version="1.28.2", latest_version=
2929
- Value `1`: Cluster is running the latest version from the specified channel
3030
- Value `0`: Cluster is not running the latest version (update available)
3131

32+
### Supported Channels
33+
34+
version-checker uses official Kubernetes release channels:
35+
36+
- `stable` - Latest stable Kubernetes release (recommended)
37+
- `latest` - Latest Kubernetes release (including pre-releases)
38+
- `latest-1.28` - Latest patch for Kubernetes 1.28.x
39+
- `latest-1.27` - Latest patch for Kubernetes 1.27.x
40+
3241
### Examples
3342

3443
```bash
35-
# Use the default stable channel
36-
./version-checker
44+
# Check against latest stable Kubernetes
45+
version-checker --kube-version-channel=stable
46+
47+
# Check against latest Kubernetes (including alpha/beta)
48+
version-checker --kube-version-channel=latest
3749

38-
# Monitor against the latest channel
39-
./version-checker --kube-channel=latest
50+
# Check against latest 1.28.x patch
51+
version-checker --kube-version-channel=latest-1.28
4052

4153
# Monitor against a specific version channel with custom interval
4254
./version-checker --kube-channel=stable-1.28 --kube-interval=1h
43-
```
55+
```
56+
57+
### Managed Kubernetes Support
58+
59+
Works with all managed Kubernetes services:
60+
- **Amazon EKS**: Compares `v1.28.2-eks-abc123` against upstream `v1.28.2`
61+
- **Google GKE**: Compares `v1.28.2-gke.1034000` against upstream `v1.28.2`
62+
- **Azure AKS**: Compares `v1.28.2-aks-xyz789` against upstream `v1.28.2`

pkg/controller/kube_controller.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ func NewKubeReconciler(
3535
interval time.Duration,
3636
channel string,
3737
) *ClusterVersionScheduler {
38+
// If no channel is specified, return nil to indicate disabled
39+
if channel == "" {
40+
log.Info("Kubernetes version checking disabled (no channel specified)")
41+
return nil
42+
}
3843

3944
return &ClusterVersionScheduler{
40-
log: log,
45+
log: log.WithField("channel", channel),
4146
client: kubernetes.NewForConfigOrDie(config),
4247
interval: interval,
4348
metrics: metrics,
@@ -47,7 +52,7 @@ func NewKubeReconciler(
4752

4853
func (s *ClusterVersionScheduler) Start(ctx context.Context) error {
4954
go s.runScheduler(ctx)
50-
return s.reconcile(ctx)
55+
return s.reconcile()
5156
}
5257

5358
func (s *ClusterVersionScheduler) runScheduler(ctx context.Context) {
@@ -63,14 +68,14 @@ func (s *ClusterVersionScheduler) runScheduler(ctx context.Context) {
6368
s.log.Info("ClusterVersionScheduler stopping")
6469
return
6570
case <-ticker.C:
66-
if err := s.reconcile(ctx); err != nil {
71+
if err := s.reconcile(); err != nil {
6772
s.log.Error(err, "Failed to reconcile cluster version")
6873
}
6974
}
7075
}
7176
}
7277

73-
func (s *ClusterVersionScheduler) reconcile(_ context.Context) error {
78+
func (s *ClusterVersionScheduler) reconcile() error {
7479
// Get current cluster version
7580
current, err := s.client.Discovery().ServerVersion()
7681
if err != nil {
@@ -110,11 +115,21 @@ func (s *ClusterVersionScheduler) reconcile(_ context.Context) error {
110115
}
111116

112117
func getLatestVersion(channel string) (string, error) {
118+
// Always use upstream Kubernetes channels - this is the authoritative source
119+
// Platform detection is kept for logging purposes only
120+
return getLatestVersionFromUpstream(channel)
121+
}
122+
123+
func getLatestVersionFromUpstream(channel string) (string, error) {
124+
// Validate channel - only allow known Kubernetes channels
125+
if !isValidKubernetesChannel(channel) {
126+
return "", fmt.Errorf("unsupported channel: %s. Valid channels: stable, latest, latest-1.xx", channel)
127+
}
128+
113129
if !strings.HasSuffix(channel, ".txt") {
114130
channel += ".txt"
115131
}
116132

117-
// Use url.JoinPath to safely join the base URL and channel path
118133
channelURL, err := url.JoinPath(channelURLSuffix, channel)
119134
if err != nil {
120135
return "", fmt.Errorf("failed to join channel URL: %w", err)
@@ -124,18 +139,13 @@ func getLatestVersion(channel string) (string, error) {
124139
client.RetryMax = 3
125140
client.RetryWaitMin = 1 * time.Second
126141
client.RetryWaitMax = 30 * time.Second
127-
// Optional: Log using your own logrus/logr logger
128142
client.Logger = nil
129143

130144
resp, err := client.Get(channelURL)
131145
if err != nil {
132146
return "", fmt.Errorf("failed to fetch from channel URL %s: %w", channelURL, err)
133147
}
134-
defer func() {
135-
if cerr := resp.Body.Close(); cerr != nil {
136-
fmt.Printf("warning: failed to close response body: %v\n", cerr)
137-
}
138-
}()
148+
defer resp.Body.Close()
139149

140150
if resp.StatusCode != 200 {
141151
return "", fmt.Errorf("unexpected status code %d when fetching channel %s", resp.StatusCode, channel)
@@ -153,3 +163,20 @@ func getLatestVersion(channel string) (string, error) {
153163

154164
return version, nil
155165
}
166+
167+
func isValidKubernetesChannel(channel string) bool {
168+
// Only allow official Kubernetes channels
169+
validChannels := []string{"stable", "latest"}
170+
171+
// Allow latest-X.Y format
172+
if strings.HasPrefix(channel, "latest-1.") {
173+
return true
174+
}
175+
176+
for _, valid := range validChannels {
177+
if channel == valid {
178+
return true
179+
}
180+
}
181+
return false
182+
}

0 commit comments

Comments
 (0)