Skip to content
Open
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
56 changes: 43 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,55 @@
# limitations under the License.

name: Build

on:
push:
branches:
- master
pull_request:

# Cancel redundant runs on the same ref (e.g. rapid pushes to a PR branch).
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

# Minimal read-only permissions; CI does not publish anything.
permissions:
contents: read

jobs:
ci:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@master
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '^1.21.0'
- name: Run make check
run: |
export PATH=${PATH}:`go env GOPATH`/bin
go version
make check
- name: Run unit tests
run: go test ./...
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
# Reads the go directive from go.mod — no hard-coded version
go-version-file: go.mod
cache: true

- name: Lint
uses: golangci/golangci-lint-action@v9
with:
version: latest
args: --timeout=10m --modules-download-mode=vendor

- name: Check license headers
run: |
go build -o "$(go env GOPATH)/bin/addlicense" github.com/google/addlicense
addlicense -check \
-ignore ".git/**" \
-ignore "vendor/**" \
-ignore "hack/**" \
-ignore "**/*.yaml" \
-ignore "**/*.yml" \
-ignore "resources/demo-config-files/**" \
-ignore "**/*.proto" \
.

- name: Test
run: go test ./...
122 changes: 60 additions & 62 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
name: releaser
# 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.

name: Release

on:
release:
types:
- published

# Only allow one release workflow to run at a time; never cancel an in-flight
# publish since that could leave a partial release.
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false

# Minimum permissions at workflow level; the release job declares what it needs.
permissions:
contents: read

jobs:
update_kubeswitch_in_homebrew_tap:
release:
name: Release
environment: Release
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
# Required to upload release assets
contents: write
steps:
- uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '^1.21.0'
- name: Build the binary-files
id: build_binary_files
run: |
sudo apt-get update
sudo apt-get install make -y
export PATH=${PATH}:`go env GOPATH`/bin
make check
make build-switcher
echo ::set-output name=latest_release_filtered_tag::${GITHUB_REF##*/}
- name: Upload mac binaries to release
uses: AButler/upload-release-assets@v2.0
- name: Checkout
uses: actions/checkout@v4
with:
files: 'hack/switch/switcher_darwin_amd64'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload mac (ARM) binaries to release
uses: AButler/upload-release-assets@v2.0
with:
files: 'hack/switch/switcher_darwin_arm64'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload Linux binaries to release
uses: AButler/upload-release-assets@v2.0
with:
files: 'hack/switch/switcher_linux_amd64'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload Linux (ARM) binaries to release
uses: AButler/upload-release-assets@v2.0
with:
files: 'hack/switch/switcher_linux_arm64'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload Windows binaries to release
uses: AButler/upload-release-assets@v2.0
# goreleaser needs the full tag history to generate changelogs
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
files: 'hack/switch/switcher_windows_amd64.exe'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Upload switch script to release
uses: AButler/upload-release-assets@v2.0
# Reads the go directive from go.mod — no hard-coded version
go-version-file: go.mod
cache: true

- name: Run goreleaser
uses: goreleaser/goreleaser-action@v7
with:
files: 'hack/switch/switch.sh'
repo-token: ${{ secrets.GITHUB_TOKEN }}
release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}
- name: Send update with latest versions to danielfoehrkn/homebrew-switch
version: latest
args: release --clean
env:
ACCESS_TOKEN: ${{ secrets.KUBESWITCH_ACCESS_TOKEN }}
run: |
mac_sha256sum=$(sha256sum hack/switch/switcher_darwin_amd64 | awk '{print $1}')
mac_arm_sha256sum=$(sha256sum hack/switch/switcher_darwin_arm64 | awk '{print $1}')
linux_sha256sum=$(sha256sum hack/switch/switcher_linux_amd64 | awk '{print $1}')
linux_arm_sha256sum=$(sha256sum hack/switch/switcher_linux_arm64 | awk '{print $1}')
switch_script_sha256sum=$(sha256sum hack/switch/switch.sh | awk '{print $1}')
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# - name: Send update with latest versions to danielfoehrkn/homebrew-switch
# env:
# ACCESS_TOKEN: ${{ secrets.KUBESWITCH_ACCESS_TOKEN }}
# run: |
# mac_sha256sum=$(sha256sum hack/switch/switcher_darwin_amd64 | awk '{print $1}')
# mac_arm_sha256sum=$(sha256sum hack/switch/switcher_darwin_arm64 | awk '{print $1}')
# linux_sha256sum=$(sha256sum hack/switch/switcher_linux_amd64 | awk '{print $1}')
# linux_arm_sha256sum=$(sha256sum hack/switch/switcher_linux_arm64 | awk '{print $1}')
# switch_script_sha256sum=$(sha256sum hack/switch/switch.sh | awk '{print $1}')

echo '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
# echo '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'

curl -X POST https://api.github.com/repos/danielfoehrKn/homebrew-switch/dispatches \
-H 'Accept: application/vnd.github.everest-preview+json' \
-u "$ACCESS_TOKEN" \
--data '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
# curl -X POST https://api.github.com/repos/danielfoehrKn/homebrew-switch/dispatches \
# -H 'Accept: application/vnd.github.everest-preview+json' \
# -u "$ACCESS_TOKEN" \
# --data '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}'
6 changes: 5 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
version: "2"
run:
timeout: 10m
linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
formatters:
enable:
- gofmt
- goimports
11 changes: 11 additions & 0 deletions .golangci.yaml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
enable:
- errcheck
- govet
- ineffassign
- staticcheck
- unused
formatters:
enable:
- gofmt
- goimports
58 changes: 58 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

version: 2

project_name: switcher

builds:
- id: switcher
main: ./cmd
# Binary is just "switcher"; the archive name_template below applies the
# platform suffix, matching the original naming: switcher_{os}_{arch}
binary: switcher
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm64
ignore:
- goos: windows
goarch: arm64
ldflags:
- -w
-X github.com/danielfoehrkn/kubeswitch/cmd/switcher.version={{.Version}}
-X github.com/danielfoehrkn/kubeswitch/cmd/switcher.buildDate={{.Date}}

# Upload raw binaries (no archives/tarballs).
# name_template matches the original release asset naming: switcher_{os}_{arch}
# (goreleaser automatically appends .exe for windows)
archives:
- formats:
- binary
name_template: "switcher_{{ .Os }}_{{ .Arch }}"

checksum:
name_template: checksums.txt
algorithm: sha256

release:
# Upload switch.sh alongside the binaries
extra_files:
- glob: hack/switch/switch.sh

snapshot:
version_template: "{{ .Tag }}-next"
3 changes: 1 addition & 2 deletions cmd/switcher/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package switcher

import (
"fmt"
"os"

delete_context "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/delete-context"
"github.com/danielfoehrkn/kubeswitch/pkg/subcommands/history"
Expand Down Expand Up @@ -247,7 +246,7 @@ func setFlagsForContextCommands(command *cobra.Command) {
command.Flags().StringVar(
&configPath,
"config-path",
os.ExpandEnv("$HOME/.kube/switch-config.yaml"),
getDefaultConfigPath(),
"path on the local filesystem to the configuration file.")
// not used for setContext command. Makes call in switch.sh script easier (no need to exclude flag from call)
command.Flags().BoolVar(
Expand Down
4 changes: 1 addition & 3 deletions cmd/switcher/gardener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package switcher

import (
"os"

gardenercontrolplane "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/gardener"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -47,7 +45,7 @@ func init() {
controlplaneCmd.Flags().StringVar(
&configPath,
"config-path",
os.ExpandEnv("$HOME/.kube/switch-config.yaml"),
getDefaultConfigPath(),
"path on the local filesystem to the configuration file.")

gardenerCmd.AddCommand(controlplaneCmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/switcher/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {
hookLsCmd.Flags().StringVar(
&configPath,
"config-path",
os.ExpandEnv("$HOME/.kube/switch-config.yaml"),
getDefaultConfigPath(),
"path on the local filesystem to the configuration file.")

hookLsCmd.Flags().StringVar(
Expand All @@ -63,7 +63,7 @@ func init() {
hookCmd.Flags().StringVar(
&configPath,
"config-path",
os.ExpandEnv("$HOME/.kube/switch-config.yaml"),
getDefaultConfigPath(),
"path on the local filesystem to the configuration file.")

hookCmd.Flags().StringVar(
Expand Down
16 changes: 16 additions & 0 deletions cmd/switcher/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ func initialize() ([]storetypes.KubeconfigStore, *types.Config, error) {
return nil, nil, err
}
s = pluginStore
case types.StoreKindSKE:
skeStore, err := store.NewSKEStore(kubeconfigStoreFromConfig)
if err != nil {
if kubeconfigStoreFromConfig.Required != nil && !*kubeconfigStoreFromConfig.Required {
continue
}
return nil, nil, err
}
s = skeStore
default:
return nil, nil, fmt.Errorf("unknown store %q", kubeconfigStoreFromConfig.Kind)
}
Expand Down Expand Up @@ -441,6 +450,13 @@ func getKubeconfigPathFromFlag() string {
return os.ExpandEnv(kubeconfigPath)
}

func getDefaultConfigPath() string {
if env := os.Getenv("KUBESWITCHCONFIG"); env != "" {
return env
}
return os.ExpandEnv("$HOME/.kube/switch-config.yaml")
}

// isDuplicatePath searches through all kubeconfig stores in the switch-config.yaml and checks if the
// given path is already configured in any of these stores
// returns true if it is already configureed
Expand Down
Loading