Repository navigation
Track Kubernetes Channels for latest versions #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d006aae
Track Kubernetes Channels for latest versions
53b21f5
Merge branch 'main' into kubernetes-release
maria-reynoso c574db5
Fix semVer
maria-reynoso bcca8e3
update docs
maria-reynoso e33bb7e
add tests
maria-reynoso 09f0a0f
fixes
maria-reynoso ae1ad29
some improvements
maria-reynoso 47373d9
Merge branch 'main' into kubernetes-release
maria-reynoso e297dce
add unit test
maria-reynoso 75915d6
tidy
maria-reynoso ee9aaac
Cleaning up a little of the implementation
davidcollom 37c185b
Introduce levellogging for retryablehttp so logging can be controller…
davidcollom 9bbac27
Adding Unit tests for logger
davidcollom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
|
|
||
| "github.com/hashicorp/go-retryablehttp" | ||
| "github.com/jetstack/version-checker/pkg/metrics" | ||
| "github.com/jetstack/version-checker/pkg/version/semver" | ||
| ) | ||
|
|
||
| const channelURLSuffix = "https://dl.k8s.io/release/" | ||
|
|
||
| type ClusterVersionScheduler struct { | ||
| client kubernetes.Interface | ||
| log *logrus.Entry | ||
| metrics *metrics.Metrics | ||
| interval time.Duration | ||
| channel string | ||
| } | ||
|
|
||
| func NewKubeReconciler( | ||
| log *logrus.Entry, | ||
| config *rest.Config, | ||
| metrics *metrics.Metrics, | ||
| interval time.Duration, | ||
| channel string, | ||
| ) *ClusterVersionScheduler { | ||
|
|
||
| return &ClusterVersionScheduler{ | ||
| log: log, | ||
| client: kubernetes.NewForConfigOrDie(config), | ||
| interval: interval, | ||
| metrics: metrics, | ||
| channel: channel, | ||
| } | ||
| } | ||
|
|
||
| func (s *ClusterVersionScheduler) Start(ctx context.Context) error { | ||
| go s.runScheduler(ctx) | ||
| return s.reconcile(ctx) | ||
| } | ||
|
|
||
| func (s *ClusterVersionScheduler) runScheduler(ctx context.Context) { | ||
| ticker := time.NewTicker(s.interval) | ||
| defer ticker.Stop() | ||
|
|
||
| s.log.WithField("interval", s.interval).WithField("channel", s.channel). | ||
| Info("ClusterVersionScheduler started") | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| s.log.Info("ClusterVersionScheduler stopping") | ||
| return | ||
| case <-ticker.C: | ||
| if err := s.reconcile(ctx); err != nil { | ||
| s.log.Error(err, "Failed to reconcile cluster version") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (s *ClusterVersionScheduler) reconcile(_ context.Context) error { | ||
| // Get current cluster version | ||
| current, err := s.client.Discovery().ServerVersion() | ||
| if err != nil { | ||
| return fmt.Errorf("getting cluster version: %w", err) | ||
| } | ||
|
|
||
| // Get latest stable version | ||
| latest, err := getLatestStableVersion(s.channel) | ||
|
maria-reynoso marked this conversation as resolved.
Outdated
|
||
| if err != nil { | ||
| return fmt.Errorf("fetching latest stable version: %w", err) | ||
| } | ||
|
|
||
| latestSemVer := semver.Parse(latest) | ||
| currentSemVer := semver.Parse(current.GitVersion) | ||
|
|
||
| // Create version strings without metadata for comparison | ||
| currentSemVerNoMeta := fmt.Sprintf("%d.%d.%d", currentSemVer.Major(), currentSemVer.Minor(), currentSemVer.Patch()) | ||
| latestSemVerNoMeta := fmt.Sprintf("%d.%d.%d", latestSemVer.Major(), latestSemVer.Minor(), latestSemVer.Patch()) | ||
|
|
||
| // Parse the versions without metadata for comparison | ||
| currentComparable := semver.Parse(currentSemVerNoMeta) | ||
| latestComparable := semver.Parse(latestSemVerNoMeta) | ||
|
|
||
| // Register metrics! | ||
| s.metrics.RegisterKubeVersion(!currentComparable.LessThan(latestComparable), | ||
| currentSemVerNoMeta, latestSemVerNoMeta, | ||
| s.channel, | ||
| ) | ||
|
|
||
| s.log.WithFields(logrus.Fields{ | ||
| "currentVersion": currentSemVerNoMeta, | ||
| "latestStable": latestSemVerNoMeta, | ||
| "channel": s.channel, | ||
| }).Info("Cluster version check complete") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func getLatestStableVersion(channel string) (string, error) { | ||
|
maria-reynoso marked this conversation as resolved.
Outdated
|
||
| if !strings.HasSuffix(channel, ".txt") { | ||
| channel += ".txt" | ||
| } | ||
|
|
||
| // We don't need a `/` here as its should be in the channelURLSuffix | ||
| channelURL := fmt.Sprintf("%s%s", channelURLSuffix, channel) | ||
|
maria-reynoso marked this conversation as resolved.
Outdated
|
||
|
|
||
| client := retryablehttp.NewClient() | ||
| client.RetryMax = 3 | ||
| client.RetryWaitMin = 1 * time.Second | ||
| client.RetryWaitMax = 30 * time.Second | ||
| // Optional: Log using your own logrus/logr logger | ||
| client.Logger = nil | ||
|
|
||
| resp, err := client.Get(channelURL) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer resp.Body.Close() | ||
|
maria-reynoso marked this conversation as resolved.
Outdated
|
||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
|
maria-reynoso marked this conversation as resolved.
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return strings.TrimSpace(string(body)), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package metrics | ||
|
|
||
| import "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| func (m *Metrics) RegisterKubeVersion(isLatest bool, currentVersion, latestVersion, channel string) { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| isLatestF := 0.0 | ||
| if isLatest { | ||
| isLatestF = 1.0 | ||
| } | ||
|
|
||
| m.kubernetesVersion.With( | ||
| prometheus.Labels{ | ||
| "current_version": currentVersion, | ||
| "latest_version": latestVersion, | ||
| "channel": channel, | ||
| }, | ||
| ).Set(isLatestF) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.