Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the KubeOneCluster API #353

Merged
merged 16 commits into from
Apr 19, 2019
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ linters:
linters-settings:
govet:
check-shadowing: true

issues:
exclude:
- "type name will be used as kubeone.KubeOneCluster by other packages, and that stutters; consider calling this Cluster"
- "don't use underscores in Go names; func SetDefaults_KubeOneCluster should be SetDefaultsKubeOneCluster"
- "don't use underscores in Go names; func SetDefaults_Hosts should be SetDefaultsHosts"
- "don't use underscores in Go names; func SetDefaults_APIEndpoints should be SetDefaultsAPIEndpoints"
- "don't use underscores in Go names; func SetDefaults_ClusterNetwork should be SetDefaultsClusterNetwork"
- "don't use underscores in Go names; func SetDefaults_Features should be SetDefaultsFeatures"
- "don't use underscores in Go names; func SetDefaults_MachineController should be SetDefaultsMachineController"
60 changes: 60 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Force dep to vendor the code generators, which aren't imported just used at dev time.
# Picking a subpackage with Go code won't be necessary once https://github.com/golang/dep/pull/1545 is merged.
# See https://github.com/golang/dep/issues/1306#issuecomment-384678415 for more details.
required = [
"k8s.io/code-generator/cmd/defaulter-gen",
"k8s.io/code-generator/cmd/deepcopy-gen",
"k8s.io/code-generator/cmd/conversion-gen",
]

[[constraint]]
name = "github.com/Masterminds/semver"
version = "1.4.2"
Expand Down Expand Up @@ -50,7 +59,18 @@
name = "sigs.k8s.io/controller-runtime"
version = "^0.1.10"

[[override]]
name = "k8s.io/code-generator"
version = "kubernetes-1.13.1"

[prune]
go-tests = true
non-go = true
unused-packages = true

# Prevent dep from pruning build scripts and codegen templates
[[prune.project]]
name = "k8s.io/code-generator"
xmudrii marked this conversation as resolved.
Show resolved Hide resolved
non-go = false
go-tests = false
unused-packages = false
34 changes: 34 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

# Copyright 2019 The KubeOne Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

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

# The code generation script takes the following arguments:
# * generators (we use only deepcopy, conversion and defaulter)
# * output path for clientset (we don't generate clienset, therefore it's empty)
# * the internal types dir
# * the external types dir
# * group and versions to generate code for
${CODEGEN_PKG}/generate-internal-groups.sh "deepcopy,conversion,defaulter" \
"" github.com/kubermatic/kubeone/pkg/apis github.com/kubermatic/kubeone/pkg/apis \
kubeone:v1alpha1 \
--output-base "$(dirname ${BASH_SOURCE})/../../../.." \
--go-header-file ${SCRIPT_ROOT}/hack/boilerplate/boilerplate.generatego.txt
48 changes: 48 additions & 0 deletions hack/verify-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

# Copyright 2019 The KubeOne Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..

DIFFROOT="${SCRIPT_ROOT}/pkg"
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg"
_tmp="${SCRIPT_ROOT}/_tmp"

cleanup() {
rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT

cleanup

mkdir -p "${TMP_DIFFROOT}"
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"

"${SCRIPT_ROOT}/hack/update-codegen.sh"
echo "diffing ${DIFFROOT} against freshly generated codegen"
ret=0
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$?
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}"
if [[ $ret -eq 0 ]]
then
echo "${DIFFROOT} up to date."
else
echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh"
exit 1
fi
21 changes: 21 additions & 0 deletions pkg/apis/kubeone/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2019 The KubeOne Authors.

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.
*/

// +k8s:deepcopy-gen=package
// +groupName=kubeone.io

// Package kubeone is the implementation of KubeOneCluster API
package kubeone
82 changes: 82 additions & 0 deletions pkg/apis/kubeone/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2019 The KubeOne Authors.

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.
*/

package kubeone

import (
"errors"

"github.com/Masterminds/semver"
)

// Leader returns the first configured host. Only call this after
// validating the cluster config to ensure a leader exists.
func (c KubeOneCluster) Leader() (HostConfig, error) {
for _, host := range c.Hosts {
if host.IsLeader {
return host, nil
}
}
return HostConfig{}, errors.New("leader not found")
}

// Followers returns all but the first configured host. Only call
// this after validating the cluster config to ensure hosts exist.
func (c KubeOneCluster) Followers() []HostConfig {
return c.Hosts[1:]
}

// SetHostname sets the hostname for the given host
func (h *HostConfig) SetHostname(hostname string) {
h.Hostname = hostname
}

// SetOperatingSystem sets the operating system for the given host
func (h *HostConfig) SetOperatingSystem(os string) {
h.OperatingSystem = os
}

// SetLeader sets is the given host leader
func (h *HostConfig) SetLeader(leader bool) {
h.IsLeader = leader
}

// CloudProviderInTree detects is there in-tree cloud provider implementation for specified provider.
// List of in-tree provider can be found here: https://github.com/kubernetes/kubernetes/tree/master/pkg/cloudprovider
func (p CloudProviderSpec) CloudProviderInTree() bool {
switch p.Name {
case CloudProviderNameAWS, CloudProviderNameGCE, CloudProviderNameOpenStack, CloudProviderNameVSphere:
return true
default:
return false
}
}

// KubernetesCNIVersion returns kubernetes-cni package version
func (m VersionConfig) KubernetesCNIVersion() string {
s := semver.MustParse(m.Kubernetes)
c, _ := semver.NewConstraint(">= 1.13.0, <= 1.13.4")

switch {
// Validation ensures that the oldest cluster version is 1.13.0.
// Versions 1.13.0-1.13.4 uses 0.6.0, so it's safe to return 0.6.0
// if >= 1.13.0, <= 1.13.4 constraint check successes.
case c.Check(s):
return "0.6.0"
default:
return "0.7.5"
}
}
51 changes: 51 additions & 0 deletions pkg/apis/kubeone/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2019 The KubeOne Authors.

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.
*/

package kubeone

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// GroupName is the name of the group used by this API
const GroupName = "kubeone.io"

// SchemeGroupVersion is group version used to register API objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal}

var (
// SchemeBuilder points to a list of functions added to Scheme
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
// AddToScheme applies all the stored functions to the Scheme
AddToScheme = SchemeBuilder.AddToScheme
)

// Kind takes an unqualified kind and returns GroupKind
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&KubeOneCluster{})
return nil
}
Loading