-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
k8s: Use contexts instead of namespaces for flexibility #228
Open
adelbertc
wants to merge
3
commits into
getnelson:0.11.x
Choose a base branch
from
adelbertc:flexible-kubectl-context
base: 0.11.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -30,7 +30,7 @@ services: | |
branches: | ||
only: | ||
- master | ||
- 0.9.x | ||
- /0\.[0-9]+\.x/ | ||
|
||
install: | ||
- bin/install-promtool | ||
|
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package nelson | ||
|
||
import nelson.Datacenter.StackName | ||
import nelson.Infrastructure.KubernetesMode | ||
import nelson.health.{HealthCheck, HealthStatus, Passing, Failing, Unknown} | ||
|
||
import argonaut.{CursorHistory, DecodeJson, Parse} | ||
|
@@ -14,25 +13,26 @@ import fs2.Stream | |
|
||
import java.io.{ByteArrayInputStream, InputStream} | ||
import java.nio.charset.StandardCharsets.UTF_8 | ||
import java.nio.file.Path | ||
|
||
import scala.sys.process.{Process, ProcessLogger} | ||
import scala.collection.mutable.ListBuffer | ||
|
||
final class Kubectl(mode: KubernetesMode) { | ||
final class Kubectl(kubeconfig: Path) { | ||
import Kubectl._ | ||
|
||
def apply(payload: String): IO[String] = { | ||
def apply(payload: String, namespace: NamespaceName): IO[String] = { | ||
val input = IO { new ByteArrayInputStream(payload.getBytes(UTF_8)) } | ||
for { | ||
result <- exec(List("kubectl", "apply", "-f", "-"), input) | ||
result <- exec(List("kubectl", "apply", "--context", namespace.root.asString, "-f", "-"), input) | ||
output <- result.output | ||
} yield output.mkString("\n") | ||
} | ||
|
||
def delete(payload: String): IO[String] = { | ||
def delete(payload: String, namespace: NamespaceName): IO[String] = { | ||
val input = IO { new ByteArrayInputStream(payload.getBytes(UTF_8)) } | ||
for { | ||
result <- exec(List("kubectl", "delete", "-f", "-"), input) | ||
result <- exec(List("kubectl", "delete", "--context", namespace.root.asString, "-f", "-"), input) | ||
output <- result.output | ||
} yield output.mkString("\n") | ||
} | ||
|
@@ -53,7 +53,7 @@ final class Kubectl(mode: KubernetesMode) { | |
|
||
def getPods(namespace: NamespaceName, stackName: StackName): IO[List[HealthStatus]] = { | ||
implicit val healthStatusDecoder = healthStatusDecodeJson | ||
exec(List("kubectl", "get", "pods", "-l", s"stackName=${stackName.toString}", "-n", namespace.root.asString, "-o", "json"), emptyStdin) | ||
exec(List("kubectl", "get", "pods", "-l", s"stackName=${stackName.toString}", "--context", namespace.root.asString, "-o", "json"), emptyStdin) | ||
.flatMap(_.output) | ||
.flatMap { stdout => | ||
IO.fromEither(for { | ||
|
@@ -64,14 +64,14 @@ final class Kubectl(mode: KubernetesMode) { | |
} | ||
|
||
def getDeployment(namespace: NamespaceName, stackName: StackName): IO[DeploymentStatus] = | ||
exec(List("kubectl", "get", "deployment", stackName.toString, "-n", namespace.root.asString, "-o", "json"), emptyStdin) | ||
exec(List("kubectl", "get", "deployment", stackName.toString, "--context", namespace.root.asString, "-o", "json"), emptyStdin) | ||
.flatMap(_.output) | ||
.flatMap { stdout => | ||
IO.fromEither(Parse.decodeEither[DeploymentStatus](stdout.mkString("\n")).leftMap(kubectlJsonError)) | ||
} | ||
|
||
def getCronJob(namespace: NamespaceName, stackName: StackName): IO[JobStatus] = | ||
exec(List("kubectl", "get", "job", "-l", s"stackName=${stackName.toString}", "-n", namespace.root.asString, "-o", "json"), emptyStdin) | ||
exec(List("kubectl", "get", "job", "-l", s"stackName=${stackName.toString}", "--context", namespace.root.asString, "-o", "json"), emptyStdin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually this is very breaking... this assumes that the kubeconfig files people are using are structured in a particular manner, correct? This would break my setup today. |
||
.flatMap(_.output) | ||
.flatMap { stdout => | ||
IO.fromEither(for { | ||
|
@@ -81,14 +81,14 @@ final class Kubectl(mode: KubernetesMode) { | |
} | ||
|
||
def getJob(namespace: NamespaceName, stackName: StackName): IO[JobStatus] = | ||
exec(List("kubectl", "get", "job", stackName.toString, "-n", namespace.root.asString, "-o", "json"), emptyStdin) | ||
exec(List("kubectl", "get", "job", stackName.toString, "--context", namespace.root.asString, "-o", "json"), emptyStdin) | ||
.flatMap(_.output) | ||
.flatMap { stdout => | ||
IO.fromEither(Parse.decodeEither[JobStatus](stdout.mkString("\n")).leftMap(kubectlJsonError)) | ||
} | ||
|
||
private def deleteV1(namespace: String, objectType: String, name: String): IO[Output] = | ||
exec(List("kubectl", "delete", objectType, name, "-n", namespace), emptyStdin) | ||
exec(List("kubectl", "delete", objectType, name, "--context", namespace), emptyStdin) | ||
|
||
private def exec(cmd: List[String], stdin: IO[InputStream]): IO[Output] = { | ||
// We need the new cats-effect resource safety hotness.. | ||
|
@@ -98,7 +98,7 @@ final class Kubectl(mode: KubernetesMode) { | |
stdout <- IO(ListBuffer.empty[String]) | ||
stderr <- IO(ListBuffer.empty[String]) | ||
logger <- IO(ProcessLogger(sout => { stdout += sout; () }, serr => { stderr += serr; () })) | ||
exitCode <- IO((Process(cmd, None, mode.environment: _*) #< is).run(logger).exitValue) | ||
exitCode <- IO((Process(cmd, None, ("KUBECONFIG", kubeconfig.toString)) #< is).run(logger).exitValue) | ||
} yield Output(stdout.toList, stderr.toList, exitCode) | ||
} | ||
}, is => IO(is.close())) | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will also break any existing configuration files in the field.