Skip to content

Commit

Permalink
Cleanup for revive
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jan 28, 2023
1 parent ed40242 commit 122f36c
Show file tree
Hide file tree
Showing 92 changed files with 488 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ linters:
- nilerr
- nolintlint
- prealloc
# - revive
- revive
# - rowserrcheck
- staticcheck
- stylecheck
Expand Down
1 change: 1 addition & 0 deletions cmd/kwok/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main is the entry point for the kwok binary.
package main

import (
Expand Down
1 change: 1 addition & 0 deletions cmd/kwokctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main is the entry point for the kwokctl binary.
package main

import (
Expand Down
4 changes: 4 additions & 0 deletions hack/verify-boilerplate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build ignore
// +build ignore

/*
Copyright 2022 The Kubernetes Authors.
Expand All @@ -14,6 +17,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main implements a simple boilerplate checker.
package main

import (
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package apis contains API Schema definitions
package apis
9 changes: 9 additions & 0 deletions pkg/apis/internalversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
"sigs.k8s.io/kwok/pkg/apis/v1alpha1"
)

// Convert_v1alpha1_KwokctlConfigurationOptions_To_internalversion_KwokctlConfigurationOptions is an autogenerated conversion function.
//
//nolint:revive
func Convert_v1alpha1_KwokctlConfigurationOptions_To_internalversion_KwokctlConfigurationOptions(in *v1alpha1.KwokctlConfigurationOptions, out *KwokctlConfigurationOptions, s conversion.Scope) error {
return autoConvert_v1alpha1_KwokctlConfigurationOptions_To_internalversion_KwokctlConfigurationOptions(in, out, s)
}

// ConvertToV1alpha1KwokctlConfiguration converts an internal version KwokctlConfiguration to a v1alpha1.KwokctlConfiguration.
func ConvertToV1alpha1KwokctlConfiguration(in *KwokctlConfiguration) (*v1alpha1.KwokctlConfiguration, error) {
var out v1alpha1.KwokctlConfiguration
out.APIVersion = v1alpha1.GroupVersion.String()
Expand All @@ -37,6 +41,7 @@ func ConvertToV1alpha1KwokctlConfiguration(in *KwokctlConfiguration) (*v1alpha1.
return &out, nil
}

// ConvertToInternalVersionKwokctlConfiguration converts a v1alpha1.KwokctlConfiguration to an internal version.
func ConvertToInternalVersionKwokctlConfiguration(in *v1alpha1.KwokctlConfiguration) (*KwokctlConfiguration, error) {
var out KwokctlConfiguration
err := Convert_v1alpha1_KwokctlConfiguration_To_internalversion_KwokctlConfiguration(in, &out, nil)
Expand All @@ -46,6 +51,7 @@ func ConvertToInternalVersionKwokctlConfiguration(in *v1alpha1.KwokctlConfigurat
return &out, nil
}

// ConvertToV1alpha1KwokConfiguration converts an internal version KwokConfiguration to a v1alpha1.KwokConfiguration.
func ConvertToV1alpha1KwokConfiguration(in *KwokConfiguration) (*v1alpha1.KwokConfiguration, error) {
var out v1alpha1.KwokConfiguration
out.APIVersion = v1alpha1.GroupVersion.String()
Expand All @@ -57,6 +63,7 @@ func ConvertToV1alpha1KwokConfiguration(in *KwokConfiguration) (*v1alpha1.KwokCo
return &out, nil
}

// ConvertToInternalVersionKwokConfiguration converts a v1alpha1.KwokConfiguration to an internal version.
func ConvertToInternalVersionKwokConfiguration(in *v1alpha1.KwokConfiguration) (*KwokConfiguration, error) {
var out KwokConfiguration
err := Convert_v1alpha1_KwokConfiguration_To_internalversion_KwokConfiguration(in, &out, nil)
Expand All @@ -66,6 +73,7 @@ func ConvertToInternalVersionKwokConfiguration(in *v1alpha1.KwokConfiguration) (
return &out, nil
}

// ConvertToV1alpha1Stage converts an internal version Stage to a v1alpha1.Stage.
func ConvertToV1alpha1Stage(in *Stage) (*v1alpha1.Stage, error) {
var out v1alpha1.Stage
out.APIVersion = v1alpha1.GroupVersion.String()
Expand All @@ -77,6 +85,7 @@ func ConvertToV1alpha1Stage(in *Stage) (*v1alpha1.Stage, error) {
return &out, nil
}

// ConvertToInternalVersionStage converts a v1alpha1.Stage to an internal version.
func ConvertToInternalVersionStage(in *v1alpha1.Stage) (*Stage, error) {
var out Stage
err := Convert_v1alpha1_Stage_To_internalversion_Stage(in, &out, nil)
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/internalversion/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ limitations under the License.
// +k8s:defaulter-gen=TypeMeta
// +k8s:conversion-gen=sigs.k8s.io/kwok/pkg/apis/v1alpha1

// Package internal implements the internal apiVersion of kwok's
// configuration
// Package internalversion implements the internal apiVersion of kwok's configuration
package internalversion
1 change: 1 addition & 0 deletions pkg/apis/internalversion/kwok_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type KwokConfiguration struct {
Options KwokConfigurationOptions
}

// KwokConfigurationOptions holds information about the options.
type KwokConfigurationOptions struct {
// The default IP assigned to the Pod on maintained Nodes.
CIDR string
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/internalversion/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type KwokctlConfiguration struct {
Components []Component
}

// KwokctlConfigurationOptions holds information about the options.
type KwokctlConfigurationOptions struct {

// KubeApiserverPort is the port to expose apiserver.
Expand Down Expand Up @@ -170,6 +171,7 @@ type KwokctlConfigurationOptions struct {
CacheDir string
}

// Component is a component of the cluster.
type Component struct {
// Name of the component specified as a DNS_LABEL.
// Each component must have a unique name (DNS_LABEL).
Expand Down Expand Up @@ -243,6 +245,7 @@ const (
ProtocolSCTP Protocol = "SCTP"
)

// Volume represents a volume that is accessible to the containers running in a component.
type Volume struct {
// Name of the volume specified.
Name string
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/internalversion/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var (
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder
)
1 change: 1 addition & 0 deletions pkg/apis/internalversion/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type SelectorRequirement struct {
// SelectorOperator is a label selector operator is the set of operators that can be used in a selector requirement.
type SelectorOperator string

// The following are valid selector operators.
var (
SelectorOpIn SelectorOperator = "In"
SelectorOpNotIn SelectorOperator = "NotIn"
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ limitations under the License.
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta

// Package v1alpha1 implements the v1alpha1 apiVersion of kwok's
// configuration
// Package v1alpha1 implements the v1alpha1 apiVersion of kwok's configuration
package v1alpha1
2 changes: 2 additions & 0 deletions pkg/apis/v1alpha1/kwok_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

const (
// KwokConfigurationKind is the kind of the KwokConfiguration.
KwokConfigurationKind = "KwokConfiguration"
)

Expand All @@ -36,6 +37,7 @@ type KwokConfiguration struct {
Options KwokConfigurationOptions `json:"options,omitempty"`
}

// KwokConfigurationOptions holds information about the options.
type KwokConfigurationOptions struct {
// The default IP assigned to the Pod on maintained Nodes.
// is the default value for flag --cidr
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

const (
// KwokctlConfigurationKind is the kind of the kwokctl configuration.
KwokctlConfigurationKind = "KwokctlConfiguration"

// ModeStableFeatureGateAndAPI is intended to reduce cluster configuration requirements
Expand All @@ -42,6 +43,7 @@ type KwokctlConfiguration struct {
Components []Component `json:"components,omitempty"`
}

// KwokctlConfigurationOptions holds information about the options.
type KwokctlConfigurationOptions struct {
// KubeApiserverPort is the port to expose apiserver.
// is the default value for flag --kube-apiserver-port and env KWOK_KUBE_APISERVER_PORT
Expand Down Expand Up @@ -264,6 +266,7 @@ type KwokctlConfigurationOptions struct {
CacheDir string `json:"cacheDir,omitempty"`
}

// Component is a component of the cluster.
type Component struct {
// Name of the component specified as a DNS_LABEL.
// Each component must have a unique name (DNS_LABEL).
Expand Down Expand Up @@ -353,6 +356,7 @@ const (
ProtocolSCTP Protocol = "SCTP"
)

// Volume represents a volume that is accessible to the containers running in a component.
type Volume struct {
// Name of the volume specified.
// +optional
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/v1alpha1/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

const (
// StageKind is the kind of the Stage resource.
StageKind = "Stage"
)

Expand Down Expand Up @@ -149,6 +150,7 @@ type SelectorRequirement struct {
// SelectorOperator is a label selector operator is the set of operators that can be used in a selector requirement.
type SelectorOperator string

// The following are valid selector operators.
var (
SelectorOpIn SelectorOperator = "In"
SelectorOpNotIn SelectorOperator = "NotIn"
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/compatibility/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package compatibility provides compatible for old version of kwokctl.
package compatibility

import (
"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/apis/v1alpha1"
)

// Config is the old configuration for kwokctl.
type Config struct {
Name string `json:"name,omitempty"`
Workdir string `json:"workdir,omitempty"`
Expand Down Expand Up @@ -82,6 +84,9 @@ type Config struct {
RuntimeConfig string `json:"kube_runtime_config,omitempty"`
}

// Convert_Config_To_internalversion_KwokctlConfiguration converts a Config to an internalversion.KwokctlConfiguration.
//
//nolint:revive
func Convert_Config_To_internalversion_KwokctlConfiguration(in *Config) (*internalversion.KwokctlConfiguration, bool) {
if in.Name == "" || in.Workdir == "" || in.Runtime == "" {
return nil, false
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/kwok/pkg/log"
)

// Load loads the given path into the context.
func Load(ctx context.Context, path ...string) ([]metav1.Object, error) {
var raws []json.RawMessage

Expand Down Expand Up @@ -147,6 +148,7 @@ func Load(ctx context.Context, path ...string) ([]metav1.Object, error) {
return objs, nil
}

// Save saves the given objects to the given path.
func Save(ctx context.Context, path string, objs []metav1.Object) error {
err := os.MkdirAll(filepath.Dir(path), 0750)
if err != nil {
Expand Down Expand Up @@ -208,6 +210,7 @@ func Save(ctx context.Context, path string, objs []metav1.Object) error {
return nil
}

// FilterWithType returns a list of objects with the given type.
func FilterWithType[T metav1.Object](objs []metav1.Object) (out []T) {
for _, obj := range objs {
o, ok := obj.(T)
Expand All @@ -218,6 +221,7 @@ func FilterWithType[T metav1.Object](objs []metav1.Object) (out []T) {
return out
}

// FilterWithoutType filters out objects of the given type.
func FilterWithoutType[T metav1.Object](objs []metav1.Object) (out []metav1.Object) {
for _, obj := range objs {
_, ok := obj.(T)
Expand All @@ -228,6 +232,7 @@ func FilterWithoutType[T metav1.Object](objs []metav1.Object) (out []metav1.Obje
return out
}

// FilterWithTypeFromContext returns all objects of the given type from the context.
func FilterWithTypeFromContext[T metav1.Object](ctx context.Context) (out []T) {
v := ctx.Value(configCtx(0))
objs, ok := v.([]metav1.Object)
Expand All @@ -237,6 +242,7 @@ func FilterWithTypeFromContext[T metav1.Object](ctx context.Context) (out []T) {
return FilterWithType[T](objs)
}

// FilterWithoutTypeFromContext returns all objects from the context that are not of the given type.
func FilterWithoutTypeFromContext[T metav1.Object](ctx context.Context) (out []metav1.Object) {
v := ctx.Value(configCtx(0))
objs, ok := v.([]metav1.Object)
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2023 The Kubernetes 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 config provides utilities to get configuration.
package config
1 change: 1 addition & 0 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

type configCtx int

// InitFlags initializes the flags for the configuration.
func InitFlags(ctx context.Context, flags *pflag.FlagSet) (context.Context, error) {
defaultConfigPath := path.Join(WorkDir, consts.ConfigName)
config := flags.StringArrayP("config", "c", []string{defaultConfigPath}, "config path")
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ var (
ClustersDir = path.Join(WorkDir, "clusters")
)

// ClusterName returns the cluster name.
func ClusterName(name string) string {
return fmt.Sprintf("%s-%s", consts.ProjectName, name)
}

// GetKwokctlConfiguration get the configuration of the kwokctl.
func GetKwokctlConfiguration(ctx context.Context) (conf *internalversion.KwokctlConfiguration) {
configs := FilterWithTypeFromContext[*internalversion.KwokctlConfiguration](ctx)
if len(configs) != 0 {
Expand All @@ -77,6 +79,7 @@ func GetKwokctlConfiguration(ctx context.Context) (conf *internalversion.Kwokctl
return conf
}

// GetKwokConfiguration get the configuration of the kwok.
func GetKwokConfiguration(ctx context.Context) (conf *internalversion.KwokConfiguration) {
configs := FilterWithTypeFromContext[*internalversion.KwokConfiguration](ctx)
if len(configs) != 0 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
// Package consts defines the constants for building the project.
package consts

// The following constants are used to build the project.
var (
ProjectName = "kwok"
ConfigName = "kwok.yaml"
Expand Down Expand Up @@ -44,6 +45,7 @@ var (
KindNodeImagePrefix = "docker.io/kindest"
)

// The following runtime is provided.
const (
RuntimeTypeKind = "kind"
RuntimeTypeDocker = "docker"
Expand Down
3 changes: 3 additions & 0 deletions pkg/kwok/cni/cni_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
gocni "github.com/containerd/go-cni"
)

// SupportedCNI returns true if the CNI plugin is supported on the current platform.
func SupportedCNI() bool {
return true
}

// Setup sets up the CNI network for the given container.
func Setup(ctx context.Context, id, name, namespace string) (ip []string, err error) {
netns, err := NewNS(id)
if err != nil {
Expand Down Expand Up @@ -59,6 +61,7 @@ func Setup(ctx context.Context, id, name, namespace string) (ip []string, err er
return ips, nil
}

// Remove removes the CNI network for the given container.
func Remove(ctx context.Context, id, name, namespace string) (err error) {
netns, err := GetNS(id)
if err != nil {
Expand Down
Loading

0 comments on commit 122f36c

Please sign in to comment.