-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create abstraction over cluster/instancegroup for building …
…assets This abstraction should let us change the version on an instance group level.
- Loading branch information
Showing
17 changed files
with
254 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,69 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/kops/pkg/apis/kops" | ||
) | ||
|
||
// InstanceGroup is a subset of the full Cluster and InstanceGroup functionality, | ||
// that gives us some abstraction over the raw types. | ||
type InstanceGroup interface { | ||
// KubernetesVersion returns the Kubernetes version for the instance group | ||
KubernetesVersion() *KubernetesVersion | ||
|
||
// GetCloudProvider returns the cloud provider for the instance group | ||
GetCloudProvider() kops.CloudProviderID | ||
|
||
// ClusterSpec returns the cluster spec for the instance group. | ||
// If possible, prefer abstracted methods over accessing this data directly. | ||
ClusterSpec() *kops.ClusterSpec | ||
} | ||
|
||
// ForInstanceGroup creates an InstanceGroup model for the given cluster and instance group. | ||
func ForInstanceGroup(cluster *kops.Cluster, ig *kops.InstanceGroup) (InstanceGroup, error) { | ||
kubernetesVersionString := cluster.Spec.KubernetesVersion | ||
kubernetesVersion, err := ParseKubernetesVersion(kubernetesVersionString) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing Kubernetes version %q: %v", kubernetesVersionString, err) | ||
} | ||
|
||
return &instanceGroupModel{cluster: cluster, ig: ig, kubernetesVersion: kubernetesVersion}, nil | ||
} | ||
|
||
// instanceGroupModel is a concrete implementation of InstanceGroup. | ||
type instanceGroupModel struct { | ||
cluster *kops.Cluster | ||
ig *kops.InstanceGroup | ||
kubernetesVersion *KubernetesVersion | ||
} | ||
|
||
var _ InstanceGroup = &instanceGroupModel{} | ||
|
||
func (m *instanceGroupModel) KubernetesVersion() *KubernetesVersion { | ||
return m.kubernetesVersion | ||
} | ||
|
||
func (m *instanceGroupModel) GetCloudProvider() kops.CloudProviderID { | ||
return m.cluster.GetCloudProvider() | ||
} | ||
|
||
func (m *instanceGroupModel) ClusterSpec() *kops.ClusterSpec { | ||
return &m.cluster.Spec | ||
} |
This file contains 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,89 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/blang/semver/v4" | ||
"k8s.io/kops/pkg/apis/kops/util" | ||
) | ||
|
||
// KubernetesVersion is a wrapper over semver functionality, | ||
// that offers some functionality particularly useful for kubernetes version semantics. | ||
type KubernetesVersion struct { | ||
versionString string | ||
version semver.Version | ||
} | ||
|
||
// ParseKubernetesVersion parses a Kubernetes version string and returns a KubernetesVersion object. | ||
func ParseKubernetesVersion(versionString string) (*KubernetesVersion, error) { | ||
parsedVersion, err := util.ParseKubernetesVersion(versionString) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing version %q: %v", versionString, err) | ||
} | ||
|
||
return &KubernetesVersion{versionString: versionString, version: *parsedVersion}, nil | ||
} | ||
|
||
// MustParseKubernetesVersion parses a Kubernetes version string and panics if it fails. | ||
func MustParseKubernetesVersion(versionString string) *KubernetesVersion { | ||
kubernetesVersion, err := ParseKubernetesVersion(versionString) | ||
if err != nil || kubernetesVersion == nil { | ||
panic(err) | ||
} | ||
return kubernetesVersion | ||
} | ||
|
||
func (v *KubernetesVersion) String() string { | ||
return v.versionString | ||
} | ||
|
||
// IsBaseURL checks if the version string is a URL, rather than a version identifier. | ||
// URLs are typically used for CI builds and during development. | ||
func IsBaseURL(kubernetesVersion string) bool { | ||
return strings.HasPrefix(kubernetesVersion, "http:") || strings.HasPrefix(kubernetesVersion, "https:") || strings.HasPrefix(kubernetesVersion, "memfs:") | ||
} | ||
|
||
// IsBaseURL checks if the version string is a URL, rather than a version identifier. | ||
// URLs are typically used for CI builds and during development. | ||
func (v *KubernetesVersion) IsBaseURL() bool { | ||
return IsBaseURL(v.versionString) | ||
} | ||
|
||
// IsGTE checks if the version is greater than or equal (>=) to the specified version. | ||
// It panics if the kubernetes version in the cluster is invalid, or if the version is invalid. | ||
func (v *KubernetesVersion) IsGTE(version string) bool { | ||
parsedVersion, err := util.ParseKubernetesVersion(version) | ||
if err != nil || parsedVersion == nil { | ||
panic(fmt.Sprintf("error parsing version %q: %v", version, err)) | ||
} | ||
|
||
// Ignore Pre & Build fields | ||
clusterVersion := v.version | ||
clusterVersion.Pre = nil | ||
clusterVersion.Build = nil | ||
|
||
return clusterVersion.GTE(*parsedVersion) | ||
} | ||
|
||
// IsLT checks if the version is strictly less (<) than the specified version. | ||
// It panics if the kubernetes version in the cluster is invalid, or if the version is invalid. | ||
func (v *KubernetesVersion) IsLT(version string) bool { | ||
return !v.IsGTE(version) | ||
} |
This file contains 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 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
Oops, something went wrong.