Skip to content

Commit

Permalink
initial open source (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
harveyxia authored Oct 29, 2024
1 parent c6a7f34 commit 5e1a2d7
Show file tree
Hide file tree
Showing 14 changed files with 1,096 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
lint-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- "1.22"
env:
GOPRIVATE: github.com/reddit/achilles-sdk-api

container:
image: golang:${{ matrix.go-version }}

steps:
- uses: actions/checkout@v3

- name: Workaround Git Security Warning
run: |
# Workaround a bug in github actions:
# https://github.com/actions/runner-images/issues/6775.
git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Setup access for private Go modules
run: |
git config --global url."ssh://git@github.com/".insteadOf https://github.com/
- name: Generate
run: |
# needed for running `tar -xJv` for installing shellcheck
apt-get update
apt-get install xz-utils
make generate
git status
git diff
test -z "$(git status --porcelain)"
# lint code
make lint
git status
git diff
test -z "$(git status --porcelain)"
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin/*

# Test binary, docker with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files
!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
tilt_modules/

*.kubeconfig

# local development tilt settings
tilt_config.json

# goreleaser
dist/

# terraform
.terraform/
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @reddit/achilles
45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
SHELL:=/bin/bash

PWD := $(PWD)
CONTROLLER_GEN := $(PWD)/bin/controller-gen
CONTROLLER_GEN_CMD := $(CONTROLLER_GEN)
GOSIMPORTS := $(PWD)/bin/gosimports
GOSIMPORTS_CMD := $(GOSIMPORTS)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install -modfile=tools/go.mod $(2) ;\
}
endef

.PHONY: manifests
manifests: $(CONTROLLER_GEN) $(GOSIMPORTS)
$(CONTROLLER_GEN_CMD) object paths="./api/..."
# avoid diff from controller-gen generated code
$(GOSIMPORTS_CMD) -local github.com/reddit/achilles-sdk-api -l -w .

.PHONY: generate
generate: manifests
go generate ./...

.PHONY: lint
lint: $(STATICCHECK) $(GOSIMPORTS)
cd tools && go mod tidy
go mod tidy
go fmt ./...
go list ./... | xargs go vet
go list ./... | xargs $(STATICCHECK_CMD)
$(GOSIMPORTS_CMD) -local github.com/reddit/achilles-sdk-api -l -w .

$(CONTROLLER_GEN):
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen)

$(GOSIMPORTS):
$(call go-get-tool,$(GOSIMPORTS),github.com/rinchsan/gosimports/cmd/gosimports)

$(STATICCHECK):
$(call go-get-tool,$(STATICCHECK),honnef.co/go/tools/cmd/staticcheck)
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# achilles-sdk-api

API types consumed by [`reddit/achilles-sdk`](https://github.snooguts.net/reddit/achilles-sdk).

This repo should minimize dependencies on external Go modules and if it must import external modules, it _must not_ use
any runtime logic.

This is to ensure that the structs, interfaces, and types exported by this module can be used in a variety of consuming
projects without causing dependency conflicts and thus forcing particular versions of commonly imported Kubernetes modules
(e.g. `github.com/kubernetes-sigs/controller-runtime`, `github.com/kubernetes/client-go`).
107 changes: 107 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package api

import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ClusterObjectRef references an object by name, namespace, and cluster.
// Used in multi-cluster APIs.
type ClusterObjectRef struct {
// Name of the object. Required.
Name string `json:"name"`

// Namespace of the object. Required.
Namespace string `json:"namespace"`

// ClusterID of the object. Required.
ClusterID string `json:"clusterId"`
}

// String returns the ClusterObjectRef as a string
func (o ClusterObjectRef) String() string {
return strings.Join([]string{o.ClusterID, o.Namespace, o.Name}, string(types.Separator))
}

// ObjectRef references a namespace-scoped object by name and namespace.
type ObjectRef struct {
// Name of the object. Required.
Name string `json:"name"`

// Namespace of the object. Required.
Namespace string `json:"namespace"`
}

// ObjectKey returns the ObjectRef as a client.ObjectKey
func (o ObjectRef) ObjectKey() client.ObjectKey {
return client.ObjectKey{Namespace: o.Namespace, Name: o.Name}
}

// ObjectRefFrom returns an *ObjectRef from a client.Object
func ObjectRefFrom(o client.Object) *ObjectRef {
return &ObjectRef{
Name: o.GetName(),
Namespace: o.GetNamespace(),
}
}

// TypedObjectRef references an object by name and namespace and includes its Group, Version, and Kind.
type TypedObjectRef struct {

// Group of the object. Required.
Group string `json:"group"`

// Version of the object. Required.
Version string `json:"version"`

// Kind of the object. Required.
Kind string `json:"kind"`

// Name of the object. Required.
Name string `json:"name"`

// Namespace of the object. Required.
Namespace string `json:"namespace"`
}

func (t TypedObjectRef) GroupVersionKind() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: t.Group,
Version: t.Version,
Kind: t.Kind,
}
}

func (t TypedObjectRef) ObjectKey() client.ObjectKey {
return client.ObjectKey{
Namespace: t.Namespace,
Name: t.Name,
}
}

func (t TypedObjectRef) ObjectKeyNotSet() bool {
return t.Name == "" && t.Namespace == ""
}

// ToCoreV1ObjectReference is a convenience method that returns a *corev1.ObjectReference with a subset of fields populated.
func (t TypedObjectRef) ToCoreV1ObjectReference() *corev1.ObjectReference {
return &corev1.ObjectReference{
Kind: t.Kind,
Name: t.Name,
Namespace: t.Namespace,
APIVersion: v1.GroupVersion{
Group: t.Group,
Version: t.Version,
}.String(),
}
}

func (t TypedObjectRef) String() string {
return fmt.Sprintf("%s: %s", t.GroupVersionKind(), t.ObjectKey())
}
Loading

0 comments on commit 5e1a2d7

Please sign in to comment.