-
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.
perf(kubernetes): Improve performance of API group and version (#3831)
* fix(kubernetes): Fix equality check in KubernetesKind We're incorrectly comparing two objects using == instead of .equals(). This is working currently because the we only ever have one KubernetesApiGroup object for each class, but the next commit will change that. * perf(kubernetes): Improve performance of API group and version These two classes each use a static variable to hold all instances of the class that are ever created. Before creating a new instance of the class, they check to see if there is already a matching instance and if so return that instance instead of creating a new one. While this might be a reasonable pattern for objects that are expensive to create, here the cost of keeping this object pool is extremely high vs. the cost of just creating an object each time we need it. Partly this is because we don't use an efficient data structure for storing exiting objects, and need to loop over an array of all existing objects each time we want to fetch/create a new one. This is compounded by the fact that that loop is in a synchronized block for thread safety. While we could make this object pool more efficient by using a better data structure and handling synchronization better, as noted above these objects are so cheap to create that it's not worth the effort. Also add tests to the class as part of this refactor.
- Loading branch information
Showing
5 changed files
with
204 additions
and
73 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
72 changes: 72 additions & 0 deletions
72
...com/netflix/spinnaker/clouddriver/kubernetes/v2/description/KubernetesApiGroupSpec.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,72 @@ | ||
/* | ||
* Copyright 2019 Google, Inc. | ||
* | ||
* 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.description | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiGroup | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class KubernetesApiGroupSpec extends Specification { | ||
@Unroll | ||
void "creates built-in API groups by name"() { | ||
when: | ||
def apiGroup = KubernetesApiGroup.fromString(name) | ||
|
||
then: | ||
apiGroup.equals(expectedApiGroup) | ||
|
||
where: | ||
name | expectedApiGroup | ||
null | KubernetesApiGroup.NONE | ||
"" | KubernetesApiGroup.NONE | ||
"batch" | KubernetesApiGroup.BATCH | ||
"BATCH" | KubernetesApiGroup.BATCH | ||
"settings.k8s.io" | KubernetesApiGroup.SETTINGS_K8S_IO | ||
"seTtiNgs.k8S.IO" | KubernetesApiGroup.SETTINGS_K8S_IO | ||
} | ||
|
||
@Unroll | ||
void "creates custom API groups"() { | ||
when: | ||
def apiGroup = KubernetesApiGroup.fromString(name) | ||
|
||
then: | ||
noExceptionThrown() | ||
apiGroup.toString() == expectedName | ||
|
||
where: | ||
name | expectedName | ||
"test.api.group" | "test.api.group" | ||
"TEST.api.Group" | "test.api.group" | ||
} | ||
|
||
@Unroll | ||
void "returns whether an API group is a native group"() { | ||
when: | ||
def apiGroup = KubernetesApiGroup.fromString(name) | ||
|
||
then: | ||
apiGroup.isNativeGroup() == isNative | ||
|
||
where: | ||
name | isNative | ||
"test.api.group" | false | ||
"batch" | true | ||
"apps" | true | ||
"" | true | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...m/netflix/spinnaker/clouddriver/kubernetes/v2/description/KubernetesApiVersionSpec.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,73 @@ | ||
/* | ||
* Copyright 2019 Google, Inc. | ||
* | ||
* 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.description | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiGroup | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiVersion | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class KubernetesApiVersionSpec extends Specification { | ||
@Unroll | ||
void "creates built-in API versions by name"() { | ||
when: | ||
def apiVersion = KubernetesApiVersion.fromString(name) | ||
|
||
then: | ||
apiVersion.equals(expectedApiGroup) | ||
|
||
where: | ||
name | expectedApiGroup | ||
null | KubernetesApiVersion.NONE | ||
"" | KubernetesApiVersion.NONE | ||
"v1" | KubernetesApiVersion.V1 | ||
"network.k8s.io/v1beta1" | KubernetesApiVersion.NETWORKING_K8S_IO_V1BETA1 | ||
"neTwoRk.k8s.io/v1beTA1" | KubernetesApiVersion.NETWORKING_K8S_IO_V1BETA1 | ||
} | ||
|
||
@Unroll | ||
void "creates custom API versions"() { | ||
when: | ||
def apiVersion = KubernetesApiVersion.fromString(name) | ||
|
||
then: | ||
noExceptionThrown() | ||
apiVersion.toString() == expectedName | ||
|
||
where: | ||
name | expectedName | ||
"test.api.group" | "test.api.group" | ||
"test.api.group/version" | "test.api.group/version" | ||
} | ||
|
||
@Unroll | ||
void "correctly parses the group from the version"() { | ||
when: | ||
def apiVersion = KubernetesApiVersion.fromString(name) | ||
|
||
then: | ||
apiVersion.getApiGroup().equals(expectedGroup) | ||
|
||
where: | ||
name | expectedGroup | ||
null | KubernetesApiGroup.NONE | ||
"" | KubernetesApiGroup.NONE | ||
"test.api.group" | KubernetesApiGroup.NONE | ||
"test.api.group/version" | KubernetesApiGroup.fromString("test.api.group") | ||
"apps/v1beta1" | KubernetesApiGroup.APPS | ||
} | ||
} |