Skip to content

Commit

Permalink
Merge pull request #37 from kubecost/mmd/update-k8s-libs
Browse files Browse the repository at this point in the history
Update K8s library versions to v0.20.4
  • Loading branch information
michaelmdresser authored Feb 16, 2022
2 parents 578ef72 + 319c73c commit c2cca15
Show file tree
Hide file tree
Showing 29 changed files with 461 additions and 309 deletions.
10 changes: 10 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Development

## Generating turndown schedule CRD code

To re-generate the code for the defined CRDs, do the following:
```sh
bash generate.sh
```

The script is based on https://github.com/kubernetes/sample-controller, specifically https://github.com/kubernetes/sample-controller/blob/master/hack/update-codegen.sh.
11 changes: 1 addition & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ RUN go mod download
# COPY the source code as the last step
COPY cmd/ /app/cmd
COPY pkg/ /app/pkg
COPY .git/ /app/.git

# Build the binary
RUN set -e ;\
GIT_COMMIT=`git rev-parse HEAD` ;\
GIT_DIRTY='' ;\
# for our purposes, we only care about dirty .go files ;\
if test -n "`git status --porcelain --untracked-files=no | grep '\.go'`"; then \
GIT_DIRTY='+dirty' ;\
fi ;\
cd cmd/turndown;\
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -a -installsuffix cgo \
-ldflags "-X main.gitCommit=${GIT_COMMIT}${GIT_DIRTY}" \
-o /go/bin/app
go build -a -installsuffix cgo -o /go/bin/app

FROM alpine:latest
RUN apk add --update --no-cache ca-certificates
Expand Down
103 changes: 44 additions & 59 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -1,73 +1,58 @@
#!/bin/bash
#

DIR=`pwd`
PREFIX=github.com
PACKAGE=${PREFIX}/kubecost/cluster-turndown
API=turndownschedule
VERSION=v1alpha1

# Cleanup after generation
post_clean() {
if [ -d "./${PREFIX}" ]; then
rm -rf ./${PREFIX}
fi
if [ -d "./vendor" ]; then
rm -rf ./vendor
fi
}

# Clean Generated Code
clean_generated() {
if [ -d "./pkg/generated" ]; then
rm -rf ./pkg/generated
fi
if [ -f "./pkg/apis/${API}/${VERSION}/zz_generated.deepcopy.go" ]; then
rm -f ./pkg/apis/${API}/${VERSION}/zz_generated.deepcopy.go
fi
post_clean
}
#!/usr/bin/env bash

# Argument processing
clean=0
while [[ "$#" -gt 0 ]]; do case $1 in
-c|--clean) clean=1; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
set -o errexit
set -o nounset
set -o pipefail

# Always clean before generation
clean_generated

# If we're cleaning only, exit here
if [ $clean -gt 0 ]; then
exit
fi
PACKAGE="github.com/kubecost/cluster-turndown"
API="turndownschedule"
VERSION="v1alpha1"

# This is hacky, but works. Since we reference generated code in our application,
# go mod vendor will actually complain that it doesn't know where the references are.
# In order to pull the code-generator, we copy our go mod/sum up a directory and run
# go mod vendor there, then move the vendor directory back to the root
mkdir tmp
pushd tmp
cp ../go.mod .
cp ../go.sum .
cp -R ../hack .
# Vendor so we have access to the generate-groups.sh script from the desired
# version of k8s.io/code-generator. If vendoring breaks because code is
# referencing generated code that has not yet been generated, refer to the
# old version of this script (commit 5c5e172) which did a little trick to be
# able to vendor if code was not generated.
go mod vendor
popd
mv tmp/vendor .
rm -rf tmp

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo code-generator)}

bash ${CODEGEN_PKG}/generate-groups.sh all \
bash "${CODEGEN_PKG}/generate-groups.sh" all \
${PACKAGE}/pkg/generated \
${PACKAGE}/pkg/apis \
turndownschedule:${VERSION} \
--output-base "$(dirname "${BASH_SOURCE[0]}")" \
--go-header-file hack/custom-boilerplate.go.txt
${API}:${VERSION} \
--go-header-file ./hack/custom-boilerplate.go.txt \
--output-base ${SCRIPT_ROOT}

# generate-groups.sh creates files at $PACKAGE/pkg/... because of the args
# passed in the above command. These files have to be moved to the correct
# place afterward. Unfortunately, if we try to create the files in the right
# location directly, like with the following:
# bash "${CODEGEN_PKG}/generate-groups.sh" all \
# ./pkg/generated \
# ./pkg/apis \
# ${API}:${VERSION} \
# --go-header-file ./hack/custom-boilerplate.go.txt \
# --output-base ${SCRIPT_ROOT}
#
# The import statements in the generated code will be incorrect and cause
# import errors like this:
# pkg/generated/informers/externalversions/generic.go:9:2: local import "./pkg/apis/turndownschedule/v1alpha1" in non-local package
#
# So we have to generate them in ./github.com/kubecost/cluster-turndown/pkg
# and then move them to ./pkg. Frustrating, but it does work. I believe
# this is because the code gen was designed well before go modules and this
# is supposed to work in the GOPATH world.

# Remove old generated code first
rm -r ./pkg/generated

# Then move new generated code to the right place
mv ./${PACKAGE}/pkg/generated ./pkg/generated
mv ./${PACKAGE}/pkg/apis/${API}/${VERSION}/* ./pkg/apis/${API}/${VERSION}/
mv ./${PACKAGE}/pkg/apis/${API}/${VERSION}/* ./pkg/apis/${API}/${VERSION}

post_clean
# Clean up temp directories created for/by code gen
rm -r ./vendor
rm -r ./github.com
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module github.com/kubecost/cluster-turndown
go 1.16

require (
cloud.google.com/go v0.46.3
cloud.google.com/go v0.54.0
github.com/aws/aws-sdk-go v1.28.7
github.com/google/uuid v1.1.1
github.com/google/uuid v1.1.2
github.com/imdario/mergo v0.3.8 // indirect
google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51
google.golang.org/grpc v1.21.1
k8s.io/api v0.0.0-20190913080256-21721929cffa
k8s.io/apimachinery v0.0.0-20190913075812-e119e5e154b6
k8s.io/client-go v0.0.0-20190620085101-78d2af792bab
k8s.io/code-generator v0.17.3
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154
google.golang.org/grpc v1.27.1
k8s.io/api v0.20.4
k8s.io/apimachinery v0.20.4
k8s.io/client-go v0.20.4
k8s.io/code-generator v0.20.15
k8s.io/klog v1.0.0
sigs.k8s.io/yaml v1.1.0
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit c2cca15

Please sign in to comment.