Skip to content

Commit

Permalink
[kots]: add registry to preflight and support checks
Browse files Browse the repository at this point in the history
This checks a connection can be made, based upon the configuration
given.
  • Loading branch information
Simon Emms committed Jul 1, 2022
1 parent 9a8ade8 commit b604a93
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ packages:
- components/ws-proxy:docker
- components/ide-proxy:docker
- components/kots-config-check/database:docker
- components/kots-config-check/registry:docker
- components/kots-config-check/storage:docker
- test:docker
- dev/version-manifest:app
Expand Down
29 changes: 29 additions & 0 deletions components/kots-config-check/registry/BUILD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
packages:
- name: app
type: go
srcs:
- go.mod
- go.sum
- "**/*.go"
env:
- CGO_ENABLED=0
config:
packaging: app
buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/kots-config-check/registry/cmd.Version=commit-${__git_commit}'"]
- name: docker
type: docker
deps:
- :app
argdeps:
- imageRepoBase
srcs:
- entrypoint.sh
config:
buildArgs:
VERSION: ${version}
dockerfile: leeway.Dockerfile
metadata:
helm-component: kots-config-check.registry
image:
- ${imageRepoBase}/kots-config-check/registry:${version}
- ${imageRepoBase}/kots-config-check/registry:commit-${__git_commit}
52 changes: 52 additions & 0 deletions components/kots-config-check/registry/cmd/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package cmd

import (
"net/url"

"github.com/heroku/docker-registry-client/registry"
"github.com/spf13/cobra"
)

var checkOpts struct {
Username string
Password string
ServerAddress string
InCluster bool
}

var checkCmd = &cobra.Command{
Use: "check",
Short: "Checks registry connection",
RunE: func(cmd *cobra.Command, args []string) error {
if !checkOpts.InCluster {
serverAddress, err := url.Parse(checkOpts.ServerAddress)
if err != nil {
return err
}
if serverAddress.Scheme == "" {
// If no scheme, default to HTTPS
serverAddress.Scheme = "https"
}

_, err = registry.New(serverAddress.String(), checkOpts.Username, checkOpts.Password)
if err != nil {
return err
}
}

return nil
},
}

func init() {
rootCmd.AddCommand(checkCmd)

checkCmd.Flags().StringVarP(&checkOpts.Username, "username", "u", "", "Registry username")
checkCmd.Flags().StringVarP(&checkOpts.Password, "password", "p", "", "Registry password")
checkCmd.Flags().StringVarP(&checkOpts.ServerAddress, "server-address", "s", "", "Registry server address")
checkCmd.Flags().BoolVar(&checkOpts.InCluster, "in-cluster", false, "Registry in-cluster")
}
26 changes: 26 additions & 0 deletions components/kots-config-check/registry/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package cmd

import (
"github.com/spf13/cobra"
)

var (
// ServiceName is the name we use for tracing/logging
ServiceName = "registry"
// Version of this service - set during build
Version = ""
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: ServiceName,
Short: "This validates a Docker v2 registry connection string",
}

func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
82 changes: 82 additions & 0 deletions components/kots-config-check/registry/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.

set -euo pipefail

REG_IN_CLUSTER_ENABLED="${1:-""}"
REG_USERNAME="${2:-""}"
REG_PASSWORD="${3:-""}"
REG_SERVER_ADDRESS="${4:-""}"
REG_IN_CLUSTER_S3_ENABLED="${5:-""}"
REG_STORE_LOCATION="${6:-""}"
REG_S3_ENDPOINT="${7:-""}"
REG_S3_ACCESS_KEY_ID="${8:-""}"
REG_S3_SECRET_ACCESS_KEY="${9:-""}"
REG_S3_BUCKET_NAME="${10:-""}"

connection="false"
s3connection="false"

REG_TYPE="incluster"
if [ "${REG_IN_CLUSTER_ENABLED}" == "0" ]; then
REG_TYPE="external"
fi

case "${REG_TYPE}" in
external)
echo "Using external registry"

# Check the registry connection
result=$(/app/registry \
check \
--server-address="${REG_SERVER_ADDRESS}" \
--username="${REG_USERNAME}" \
--password="${REG_PASSWORD}" || echo "fail")

if [ "${result}" != "fail" ]; then
connection="true"
fi
s3connection="true"
;;
incluster)
echo "Using in-cluster registry"
connection="true"

# This is "true" or "false" not "1" or "0"
if [ "${REG_IN_CLUSTER_S3_ENABLED}" == "true" ]; then
# The Azure and GCP arguments are ignored - use variable names so it's readable
if bash /storage.sh \
"s3" \
"${REG_STORE_LOCATION}" \
"AZURE_ACCOUNT_NAME" \
"AZURE_ACCESS_KEY" \
"GCP_PROJECT_ID" \
"GCP_SERVICE_ACCOUNT_KEY" \
"${REG_S3_ENDPOINT}" \
"${REG_S3_ACCESS_KEY_ID}" \
"${REG_S3_SECRET_ACCESS_KEY}" \
"${REG_S3_BUCKET_NAME}"; then
s3connection="true"
fi
else
s3connection="true"
fi
;;
*)
echo "Unknown registry type: '${REG_TYPE}'"
exit 1
;;
esac

if [ "${connection}" = "true" ]; then
echo "registry: ok"
else
echo "registry: error"
fi
if [ "${s3connection}" = "true" ]; then
echo "s3: ok"
else
echo "s3: error"
fi
19 changes: 19 additions & 0 deletions components/kots-config-check/registry/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/gitpod-io/gitpod/kots-config-check/registry

go 1.18

require (
github.com/heroku/docker-registry-client v0.0.0-20211012143308-9463674c8930
github.com/spf13/cobra v1.5.0
)

require (
github.com/docker/distribution v0.0.0-20171011171712-7484e51bf6af // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 // indirect
)
Loading

0 comments on commit b604a93

Please sign in to comment.