-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(k8s): improve determineOmitKinds (#3870)
* fix(k8s): improve determineOmitKinds improved perf on determineOmitKinds while we consider longer term approaches. previously, we made a kubectl call for every kind spinnaker knew about and every custom resource, regardless of if the API could handle it or not. this approach uses 2 methods to determine readability: 1. fetch all resources the API says it knows about via `kubectl api-resources`. this allows us to build a list of all known kinds. `determineOmitKinds` first checks against this list, preventing us from making list calls to kinds we already know we can't deploy. 2. for all available kinds, use the `SelfSubjectAccessReview` API to determine if the account has access to the kind. We're checking against the `list` verb, as we were previously, but don't have to actually deserialize the entire list of manifests that a regular `kubectl list` call returns.
- Loading branch information
1 parent
dfe14a9
commit 6e30e56
Showing
5 changed files
with
215 additions
and
18 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
53 changes: 53 additions & 0 deletions
53
...com/netflix/spinnaker/clouddriver/kubernetes/v2/security/KubernetesApiResourceParser.java
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,53 @@ | ||
/* | ||
* Copyright 2019 Armory | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.v2.security; | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiGroup; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class KubernetesApiResourceParser { | ||
|
||
public static Set<KubernetesKind.ScopedKind> parse(String input) { | ||
String[] lines = input.trim().split("\n"); | ||
String headerRow = lines[0]; | ||
int nameIndex = headerRow.indexOf("NAME"); | ||
int apiGroupIndex = headerRow.indexOf("APIGROUP"); | ||
int namespaceIndex = headerRow.indexOf("NAMESPACED"); | ||
int kindIndex = headerRow.indexOf("KIND"); | ||
|
||
// we expect NAME to be at index 0 of the first row | ||
// if it isn't, then we didn't get the data in the | ||
// format we expected | ||
if (nameIndex != 0) { | ||
throw new IllegalArgumentException( | ||
"api-resources input not in the proper format. expected to find NAME header."); | ||
} | ||
|
||
Set<KubernetesKind.ScopedKind> kinds = new HashSet<>(); | ||
|
||
for (int i = 1; i < lines.length; i++) { | ||
String line = lines[i]; | ||
String apiGroup = line.substring(apiGroupIndex, namespaceIndex).trim(); | ||
String kind = line.substring(kindIndex).trim(); | ||
kinds.add(new KubernetesKind.ScopedKind(kind, KubernetesApiGroup.fromString(apiGroup))); | ||
} | ||
|
||
return kinds; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...tflix/spinnaker/clouddriver/kubernetes/v2/security/KubernetesApiResourceParserSpec.groovy
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,41 @@ | ||
/* | ||
* Copyright 2019 Armory | ||
* | ||
* 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 com.netflix.spinnaker.clouddriver.kubernetes.v2.security | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiGroup | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind | ||
import spock.lang.Specification | ||
|
||
class KubernetesApiResourceParserSpec extends Specification { | ||
|
||
def inputWithHeaders = """ | ||
NAME SHORTNAMES APIGROUP NAMESPACED KIND | ||
bindings true Binding | ||
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration | ||
""" | ||
|
||
def "parses api-resources response properly"() { | ||
when: | ||
def result = KubernetesApiResourceParser.parse(inputWithHeaders) | ||
|
||
then: | ||
[ | ||
new KubernetesKind.ScopedKind("Binding", KubernetesApiGroup.fromString("")), | ||
new KubernetesKind.ScopedKind("MutatingWebhookConfiguration", KubernetesApiGroup.fromString("admissionregistration.k8s.io")) | ||
] as Set == result | ||
} | ||
} |
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