This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides custom plugin labels for all Skaffold deployments for serve…
…r-side metrics. (#71) * Provide custom plugin labels for all Skaffold deployments.
- Loading branch information
Showing
13 changed files
with
267 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
dependencies { | ||
testCompile(project(":common-test-lib")) | ||
} |
44 changes: 44 additions & 0 deletions
44
core/src/main/kotlin/com/google/container/tools/core/PluginInfo.kt
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,44 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* 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.google.container.tools.core | ||
|
||
import com.intellij.ide.plugins.IdeaPluginDescriptor | ||
import com.intellij.ide.plugins.PluginManager | ||
import com.intellij.openapi.components.ServiceManager | ||
import com.intellij.openapi.extensions.PluginId | ||
import com.intellij.util.PlatformUtils | ||
|
||
const val CONTAINER_TOOLS_PLUGIN_ID = "com.google.container.tools" | ||
const val UNKNOWN_VERSION = "unknown" | ||
|
||
/** Utilities to get common plugin information such as its version. */ | ||
class PluginInfo { | ||
companion object { | ||
val instance: PluginInfo | ||
get() = ServiceManager.getService(PluginInfo::class.java)!! | ||
} | ||
|
||
/** Version of the plugin installed, or `N/A` if version cannot be determined. */ | ||
val pluginVersion: String by lazy { | ||
val ideaPluginDescriptor: IdeaPluginDescriptor? = | ||
PluginManager.getPlugin(PluginId.getId(CONTAINER_TOOLS_PLUGIN_ID)) | ||
|
||
ideaPluginDescriptor?.version ?: UNKNOWN_VERSION | ||
} | ||
|
||
val platformPrefix: String by lazy { PlatformUtils.getPlatformPrefix() } | ||
} |
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,22 @@ | ||
<!-- | ||
~ Copyright 2018 Google Inc. All Rights Reserved. | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<!-- Declares core/utility IDE extensions and components for Container Tools --> | ||
<idea-plugin> | ||
<extensions defaultExtensionNs="com.intellij"> | ||
<applicationService serviceImplementation="com.google.container.tools.core.PluginInfo"/> | ||
</extensions> | ||
</idea-plugin> |
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 |
---|---|---|
|
@@ -15,5 +15,6 @@ | |
*/ | ||
|
||
dependencies { | ||
compile(project(":core")) | ||
testCompile(project(":common-test-lib")) | ||
} |
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
skaffold/src/main/kotlin/com/google/container/tools/skaffold/SkaffoldLabels.kt
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 2018 Google Inc. All Rights Reserved. | ||
* | ||
* 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.google.container.tools.skaffold | ||
|
||
import com.google.common.annotations.VisibleForTesting | ||
import com.google.container.tools.core.PluginInfo | ||
import com.intellij.openapi.application.ApplicationInfo | ||
|
||
const val IDE_LABEL = "ide" | ||
const val IDE_VERSION_LABEL = "ideVersion" | ||
const val PLUGIN_VERSION_LABEL = "ijPluginVersion" | ||
|
||
/** | ||
* Maintains list of Kubernetes labels - string based key-value pairs used by Skaffold to apply | ||
* to all deployments. [getDefaultLabels] function supplies default set of labels used for all | ||
* deployments. | ||
*/ | ||
class SkaffoldLabels { | ||
companion object { | ||
/** | ||
* Default set of labels for all Skaffold-based deployments. Includes IDE type and | ||
* version, plugin version. | ||
*/ | ||
val defaultLabels: SkaffoldLabels by lazy { | ||
populateDefaultLabels() | ||
} | ||
|
||
@VisibleForTesting | ||
fun populateDefaultLabels(): SkaffoldLabels { | ||
return SkaffoldLabels().apply { | ||
this.labels[IDE_LABEL] = PluginInfo.instance.platformPrefix | ||
this.labels[IDE_VERSION_LABEL] = ApplicationInfo.getInstance().getStrictVersion() | ||
this.labels[PLUGIN_VERSION_LABEL] = PluginInfo.instance.pluginVersion | ||
} | ||
} | ||
} | ||
|
||
val labels: MutableMap<String, String> = mutableMapOf() | ||
} |
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
54 changes: 54 additions & 0 deletions
54
skaffold/src/test/kotlin/com/google/container/tools/skaffold/SkaffoldLabelsTest.kt
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,54 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* 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.google.container.tools.skaffold | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.google.container.tools.core.PluginInfo | ||
import com.google.container.tools.test.ContainerToolsRule | ||
import com.google.container.tools.test.TestService | ||
import com.intellij.openapi.application.ApplicationInfo | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
/** Unit tests for [SkaffoldLabels] class. */ | ||
class SkaffoldLabelsTest { | ||
@get:Rule | ||
val containerToolsRule = ContainerToolsRule(this) | ||
|
||
@MockK | ||
@TestService | ||
private lateinit var mockApplicationInfo: ApplicationInfo | ||
@MockK | ||
@TestService | ||
private lateinit var mockPluginInfo: PluginInfo | ||
|
||
@Test | ||
fun `valid default skaffold labels are constructed from IDE and plugin information`() { | ||
// mock all sources of information that should be used for default labels | ||
every { mockApplicationInfo.strictVersion } answers { "999.9" } | ||
every { mockPluginInfo.platformPrefix } answers { "TestIde" } | ||
every { mockPluginInfo.pluginVersion } answers { "0.1" } | ||
|
||
val defaultLabels: SkaffoldLabels = SkaffoldLabels.populateDefaultLabels() | ||
|
||
assertThat(defaultLabels.labels["ide"]).isEqualTo("TestIde") | ||
assertThat(defaultLabels.labels["ideVersion"]).isEqualTo("999.9") | ||
assertThat(defaultLabels.labels["ijPluginVersion"]).isEqualTo("0.1") | ||
} | ||
} |
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