Skip to content
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

feat: s/ksonnet-lib/k8s-alpha #563

Merged
merged 8 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions cmd/tk/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"log"
"os"
"os/exec"
"strconv"

"github.com/go-clix/cli"

"github.com/grafana/tanka/pkg/spec/v1alpha1"
)

const defaultK8sVersion = "1.20"

// initCmd creates a new application
func initCmd() *cli.Command {
cmd := &cli.Command{
Expand All @@ -22,7 +25,7 @@ func initCmd() *cli.Command {
}

force := cmd.Flags().BoolP("force", "f", false, "ignore the working directory not being empty")
installK8sLibFlag := cmd.Flags().Bool("k8s", true, "set to false to skip installation of k.libsonnet")
installK8s := cmd.Flags().String("k8s", defaultK8sVersion, "choose the version of k8s-alpha, set to false to skip")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no shorthand version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that be better? This approach keeps compatibility with --k8s=<bool> flag.

inline := cmd.Flags().BoolP("inline", "i", false, "create an inline environment")

cmd.Run = func(cmd *cli.Command, args []string) error {
Expand Down Expand Up @@ -53,15 +56,29 @@ func initCmd() *cli.Command {
return err
}

if *installK8sLibFlag {
if err := installK8sLib(); err != nil {
version := *installK8s
doInstall, err := strconv.ParseBool(*installK8s)
if doInstall && err == nil {
// --k8s=true, fallback to default version
version = defaultK8sVersion
} else {
// --k8s=<non-boolean>
doInstall = true
}

if doInstall {
if err := installK8sLib(version); err != nil {
// This is not fatal, as most of Tanka will work anyways
log.Println("Installing k.libsonnet:", err)
failed = true
}
}

fmt.Println("Directory structure set up! Remember to configure the API endpoint:\n`tk env set environments/default --server=https://127.0.0.1:6443`")
if *inline {
fmt.Println("Directory structure set up! Remember to configure the API endpoint in environments/default/main.jsonnet")
} else {
fmt.Println("Directory structure set up! Remember to configure the API endpoint:\n`tk env set environments/default --server=https://127.0.0.1:6443`")
}
if failed {
log.Println("Errors occured while initializing the project. Check the above logs for details.")
}
Expand All @@ -71,7 +88,7 @@ func initCmd() *cli.Command {
return cmd
}

func installK8sLib() error {
func installK8sLib(version string) error {
jbBinary := "jb"
if env := os.Getenv("TANKA_JB_PATH"); env != "" {
jbBinary = env
Expand All @@ -82,11 +99,11 @@ func installK8sLib() error {
}

var initialPackages = []string{
"github.com/ksonnet/ksonnet-lib/ksonnet.beta.4",
"github.com/jsonnet-libs/k8s-alpha/" + version,
"github.com/grafana/jsonnet-libs/ksonnet-util",
Duologic marked this conversation as resolved.
Show resolved Hide resolved
}

if err := writeNewFile("lib/k.libsonnet", "import 'github.com/ksonnet/ksonnet-lib/ksonnet.beta.4/k.libsonnet'\n"); err != nil {
if err := writeNewFile("lib/k.libsonnet", "import 'github.com/jsonnet-libs/k8s-alpha/"+version+"/main.libsonnet'\n"); err != nil {
return err
}

Expand Down
15 changes: 8 additions & 7 deletions docs/docs/directory-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ Tanka uses the following directories and special files:
├── jsonnetfile.json # direct dependencies
├── jsonnetfile.lock.json # all dependencies with exact versions
├── lib # libraries for this project only
│   └── k.libsonnet # alias file for vendor/github.com/ksonnet/ksonnet-lib/ksonnet.beta.4/k.libsonnet
│   └── k.libsonnet # alias file for vendor/github.com/jsonnet-libs/k8s-alpha/1.21/main.libsonnet
└── vendor # external libraries installed using jb
├── github.com
│   ├── grafana
│   │   └── jsonnet-libs
│   │   └── ksonnet-util # Grafana Labs' usability extensions to k.libsonnet
│   │   ├── ...
│   │   └── kausal.libsonnet
│   └── ksonnet
│   └── ksonnet-lib
│   └── ksonnet.beta.4 # kubernetes library
│   ├── k8s.libsonnet
│   └── k.libsonnet
├── ksonnet.beta.4 -> github.com/ksonnet/ksonnet-lib/ksonnet.beta.4
│   └── jsonnet-libs
│   └── k8s-alpha
│   └── 1.21 # kubernetes library
│   ├── ...
│   └── main.libsonnet
├── 1.21 -> github.com/jsonnet-libs/k8s-alpha/1.21
└── ksonnet-util -> github.com/grafana/jsonnet-libs/ksonnet-util
```

Expand Down
17 changes: 7 additions & 10 deletions docs/docs/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,26 @@ Below is a list of common errors and how to address them.

When migrating from `ksonnet`, this error might occur, because Tanka does not
provide the global `__ksonnet` variable, nor does it strictly have the concept
of components.
of components.
You will need to use the plain Jsonnet `import` feature instead. Note that this
requires your code to be inside of one of the
[import paths](directory-structure/#import-paths).

### Evaluating jsonnet: RUNTIME ERROR: couldn't open import "k.libsonnet": no match locally or in the Jsonnet library paths

This error can occur when the `ksonnet` kubernetes libraries are missing in the
import paths. While `ksonnet` used to magically include them, Tanka follows a
This error can occur when the `k8s-alpha` kubernetes libraries are missing in the
import paths. While `k8s-alpha` used to magically include them, Tanka follows a
more explicit approach and requires you to install them using `jb`:

```bash
$ jb install github.com/ksonnet/ksonnet-lib/ksonnet.beta.4
$ echo "import 'github.com/ksonnet/ksonnet-lib/ksonnet.beta.4/k.libsonnet'" > lib/k.libsonnet
$ jb install github.com/jsonnet-libs/k8s-alpha/1.21
$ echo "import 'github.com/jsonnet-libs/k8s-alpha/1.21/main.libsonnet'" > lib/k.libsonnet
```

This does 2 things:

1) It installs the ksonnet library (in `vendor/github.com/ksonnet/ksonnet-lib/ksonnet.beta.4`).
If you need a specific version, take a look at
https://github.com/ksonnet/ksonnet-lib. When a pre-compiled version is
available, install it using `jb`, otherwise compile it yourself and place it
under `lib/`.
1) It installs the `k8s-alpha` library (in `vendor/github.com/jsonnet-libs/k8s-alpha/1.21/`).
You can replace the `1.21` matching the Kubernetes version you want to run against.

2) It makes an alias for libraries importing `k.libsonnet` directly. See
https://tanka.dev/tutorial/k-lib#aliasing for the alias rationale.
Expand Down
22 changes: 11 additions & 11 deletions docs/docs/tutorial/environments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ For documentation purposes it is handy to have a separate file for parameters an
##### prom-grafana.libsonnet

```jsonnet
(import "ksonnet-util/kausal.libsonnet") +
local k = import "ksonnet-util/kausal.libsonnet";

(import "./config.libsonnet") +
{
local deployment = $.apps.v1.deployment,
local container = $.core.v1.container,
local port = $.core.v1.containerPort,
local service = $.core.v1.service,
local deployment = k.apps.v1.deployment,
local container = k.core.v1.container,
local port = k.core.v1.containerPort,
local service = k.core.v1.service,

// alias our params, too long to type every time
local c = $._config.promgrafana,
Expand All @@ -99,7 +100,7 @@ For documentation purposes it is handy to have a separate file for parameters an
+ container.withPorts([port.new("api", c.prometheus.port)]),
],
),
service: $.util.serviceFor(self.deployment),
service: k.util.serviceFor(self.deployment),
},

grafana: {
Expand All @@ -110,7 +111,9 @@ For documentation purposes it is handy to have a separate file for parameters an
+ container.withPorts([port.new("ui", c.grafana.port)]),
],
),
service: $.util.serviceFor(self.deployment) + service.mixin.spec.withType("NodePort"),
service:
k.util.serviceFor(self.deployment)
+ service.mixin.spec.withType("NodePort"),
},
}
}
Expand All @@ -131,16 +134,14 @@ All that's left now is importing the library and configuring it. For `dev`, the

```jsonnet
// environments/prom-grafana/dev
(import "ksonnet-util/kausal.libsonnet") +
(import "prom-grafana/prom-grafana.libsonnet")
import "prom-grafana/prom-grafana.libsonnet"
```

For `prod` however, it is a bad idea to rely on `latest` for the images .. let's
add some proper tags:

```jsonnet
// environments/prom-grafana/prod
(import "ksonnet-util/kausal.libsonnet") +
(import "prom-grafana/prom-grafana.libsonnet") +
{
// again, we only want to patch, not replace, thus +::
Expand All @@ -164,7 +165,6 @@ Here comes the already familiar `+:` (or `+::`) syntax into play. It allows to
**partially** override values of an object. Let's say we wanted to add some labels to the Prometheus `Deployment`, but our `_config` params don't allow us to. We can still do this in our `main.jsonnet`:

```jsonnet
(import "ksonnet-util/kausal.libsonnet") +
(import "prom-grafana/prom-grafana.libsonnet") +
{
promgrafana+: {
Expand Down
Loading