forked from k8stopologyawareschedwg/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request k8stopologyawareschedwg#83 from fromanirh/update-v…
…alidation validate: extend to check and store the cluster version
- Loading branch information
Showing
38 changed files
with
2,233 additions
and
43 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
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,83 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
*/ | ||
|
||
package validator | ||
|
||
import ( | ||
"k8s.io/client-go/discovery" | ||
|
||
goversion "github.com/aquasecurity/go-version/pkg/version" | ||
) | ||
|
||
const ( | ||
ExpectedMinKubeVersion = "1.21" | ||
) | ||
|
||
const ( | ||
ComponentAPIVersion = "API Version" | ||
) | ||
|
||
func (vd *Validator) ValidateClusterVersion(cli *discovery.DiscoveryClient) ([]ValidationResult, error) { | ||
ver, err := cli.ServerVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
vd.serverVersion = ver | ||
vrs := ValidateClusterVersion(ver.GitVersion) | ||
vd.results = append(vd.results, vrs...) | ||
return vrs, nil | ||
} | ||
|
||
func ValidateClusterVersion(clusterVersion string) []ValidationResult { | ||
ok, err := isAPIVersionAtLeast(clusterVersion, ExpectedMinKubeVersion) | ||
if err != nil { | ||
return []ValidationResult{ | ||
{ | ||
/* no specific nodes: all are affected! */ | ||
Area: AreaCluster, | ||
Component: ComponentAPIVersion, | ||
/* no specific Setting: implicit in the component! */ | ||
Expected: "valid version", | ||
Detected: err.Error(), | ||
}, | ||
} | ||
} | ||
if !ok { | ||
return []ValidationResult{ | ||
{ | ||
/* no specific nodes: all are affected! */ | ||
Area: AreaCluster, | ||
Component: ComponentAPIVersion, | ||
/* no specific Setting: implicit in the component! */ | ||
Expected: ExpectedMinKubeVersion, | ||
Detected: clusterVersion, | ||
}, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func isAPIVersionAtLeast(server, refver string) (bool, error) { | ||
ref, err := goversion.Parse(refver) | ||
if err != nil { | ||
return false, err | ||
} | ||
ser, err := goversion.Parse(server) | ||
if err != nil { | ||
return false, err | ||
} | ||
return ser.Compare(ref) >= 0, nil | ||
} |
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,71 @@ | ||
/* | ||
* 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. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
*/ | ||
|
||
package validator | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestClusterVersionValidations(t *testing.T) { | ||
type testCase struct { | ||
version string | ||
expected []ValidationResult | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
version: "1.23", | ||
expected: []ValidationResult{}, | ||
}, | ||
{ | ||
version: "", | ||
expected: []ValidationResult{ | ||
{ | ||
Area: AreaCluster, | ||
Component: ComponentAPIVersion, | ||
}, | ||
}, | ||
}, | ||
{ | ||
version: "INVALID", | ||
expected: []ValidationResult{ | ||
{ | ||
Area: AreaCluster, | ||
Component: ComponentAPIVersion, | ||
}, | ||
}, | ||
}, | ||
{ | ||
version: "1.10", | ||
expected: []ValidationResult{ | ||
{ | ||
Area: AreaCluster, | ||
Component: ComponentAPIVersion, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.version, func(t *testing.T) { | ||
got := ValidateClusterVersion(tc.version) | ||
if !matchValidationResults(tc.expected, got) { | ||
t.Fatalf("validation failed:\nexpected=%#v\ngot=%#v", tc.expected, got) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.