diff --git a/components/BUILD.yaml b/components/BUILD.yaml index 1408708f00b803..9ffc0a24939aa1 100644 --- a/components/BUILD.yaml +++ b/components/BUILD.yaml @@ -54,6 +54,7 @@ packages: - components/image-builder-mk3:docker - components/local-app:docker - components/public-api-server:docker + - components/usage-controller:docker - components/openvsx-proxy:docker - components/proxy:docker - components/registry-facade:docker diff --git a/components/usage-controller/BUILD.yaml b/components/usage-controller/BUILD.yaml new file mode 100644 index 00000000000000..bdff4f8ec2748a --- /dev/null +++ b/components/usage-controller/BUILD.yaml @@ -0,0 +1,30 @@ +packages: + - name: app + type: go + srcs: + - "**/*.go" + - "go.mod" + - "go.sum" + deps: + - components/common-go:lib + env: + - CGO_ENABLED=0 + - GOOS=linux + config: + packaging: app + buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/usage-controller/cmd.Version=commit-${__git_commit}'"] + - name: docker + type: docker + deps: + - :app + argdeps: + - imageRepoBase + config: + buildArgs: + VERSION: ${version} + dockerfile: leeway.Dockerfile + metadata: + helm-component: usage-controller + image: + - ${imageRepoBase}/usage-controller:${version} + - ${imageRepoBase}/usage-controller:commit-${__git_commit} diff --git a/components/usage-controller/cmd/root.go b/components/usage-controller/cmd/root.go new file mode 100644 index 00000000000000..4ea94816fc7f11 --- /dev/null +++ b/components/usage-controller/cmd/root.go @@ -0,0 +1,33 @@ +// 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 ( + "context" + "os" + + "github.com/gitpod-io/gitpod/common-go/log" + "github.com/spf13/cobra" +) + +var ( + // ServiceName is the name we use for tracing/logging + ServiceName = "usage-controller" + // 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: "Runs usage controller", +} + +func Execute() { + if err := rootCmd.ExecuteContext(context.Background()); err != nil { + log.WithError(err).Error("Failed to execute command.") + os.Exit(1) + } +} diff --git a/components/usage-controller/cmd/run.go b/components/usage-controller/cmd/run.go new file mode 100644 index 00000000000000..93f167986546bc --- /dev/null +++ b/components/usage-controller/cmd/run.go @@ -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 ( + "github.com/gitpod-io/gitpod/common-go/log" + "github.com/spf13/cobra" + "os" + "os/signal" + "syscall" +) + +func init() { + rootCmd.AddCommand(run()) +} + +func run() *cobra.Command { + var ( + verbose bool + ) + + cmd := &cobra.Command{ + Use: "run", + Short: "Starts the service", + Version: Version, + Run: func(cmd *cobra.Command, args []string) { + log.Init(ServiceName, Version, true, verbose) + + log.Info("Hello world usage-controller") + + done := make(chan bool, 1) + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + go func() { + <-sigs + log.Info("Received termination signal") + done <- true + }() + + log.Info("Awaiting signal to terminate...") + <-done + log.Info("Exiting.") + }, + } + + cmd.Flags().BoolVar(&verbose, "verbose", false, "Toggle verbose logging (debug level)") + + return cmd +} diff --git a/components/usage-controller/go.mod b/components/usage-controller/go.mod new file mode 100644 index 00000000000000..04191803f54b1c --- /dev/null +++ b/components/usage-controller/go.mod @@ -0,0 +1,67 @@ +module github.com/gitpod-io/gitpod/usage-controller + +go 1.18 + +replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway + +replace k8s.io/api => k8s.io/api v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/apimachinery => k8s.io/apimachinery v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/apiserver => k8s.io/apiserver v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/cli-runtime => k8s.io/cli-runtime v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/client-go => k8s.io/client-go v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/cloud-provider => k8s.io/cloud-provider v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/code-generator => k8s.io/code-generator v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/component-base => k8s.io/component-base v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/cri-api => k8s.io/cri-api v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kube-proxy => k8s.io/kube-proxy v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kubelet => k8s.io/kubelet v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/metrics => k8s.io/metrics v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/component-helpers => k8s.io/component-helpers v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/controller-manager => k8s.io/controller-manager v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/kubectl => k8s.io/kubectl v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/mount-utils => k8s.io/mount-utils v0.23.5 // leeway indirect from components/common-go:lib + +replace k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.23.5 // leeway indirect from components/common-go:lib + +require ( + github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000 + github.com/spf13/cobra v1.4.0 +) + +require ( + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect +) diff --git a/components/usage-controller/go.sum b/components/usage-controller/go.sum new file mode 100644 index 00000000000000..e6f27b2c7ae0da --- /dev/null +++ b/components/usage-controller/go.sum @@ -0,0 +1,23 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/components/usage-controller/leeway.Dockerfile b/components/usage-controller/leeway.Dockerfile new file mode 100644 index 00000000000000..039f732da07076 --- /dev/null +++ b/components/usage-controller/leeway.Dockerfile @@ -0,0 +1,23 @@ +# Copyright (c) 2020 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. + +FROM alpine:3.15 + +# Ensure latest packages are present, like security updates. +RUN apk upgrade --no-cache \ + && apk add --no-cache ca-certificates + +RUN adduser -S -D -H -h /app -u 1000 appuser +COPY components-usage-controller--app/usage-controller /app/usage-controller +RUN chown -R appuser /app + +USER appuser + +ARG __GIT_COMMIT +ARG VERSION + +ENV GITPOD_BUILD_GIT_COMMIT=${__GIT_COMMIT} +ENV GITPOD_BUILD_VERSION=${VERSION} +ENTRYPOINT [ "/app/usage-controller" ] +CMD [ "-v", "help" ] diff --git a/components/usage-controller/main.go b/components/usage-controller/main.go new file mode 100644 index 00000000000000..60a50def47639d --- /dev/null +++ b/components/usage-controller/main.go @@ -0,0 +1,11 @@ +// 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 main + +import "github.com/gitpod-io/gitpod/usage-controller/cmd" + +func main() { + cmd.Execute() +}