Skip to content

Commit

Permalink
Fix golint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravkghildiyal committed Apr 18, 2024
1 parent ea8d1c9 commit 12b3379
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 77 deletions.
2 changes: 1 addition & 1 deletion gwctl/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runDescribe(cmd *cobra.Command, args []string, params *utils.CmdParams) {
}
}
policiesPrinter.PrintPoliciesDescribeView(policyList)

case "policycrd", "policycrds":
var policyCrdList []policymanager.PolicyCRD
if len(args) == 1 {
Expand Down
29 changes: 22 additions & 7 deletions gwctl/pkg/common/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -51,9 +52,15 @@ func NewK8sClients(kubeconfig string) (*K8sClients, error) {
if err != nil {
return nil, fmt.Errorf("failed to initialize Kubernetes client: %v", err)
}
gatewayv1alpha2.AddToScheme(client.Scheme())
gatewayv1beta1.AddToScheme(client.Scheme())
gatewayv1.AddToScheme(client.Scheme())
if err := gatewayv1alpha2.AddToScheme(client.Scheme()); err != nil {
return nil, err
}
if err := gatewayv1beta1.AddToScheme(client.Scheme()); err != nil {
return nil, err
}
if err := gatewayv1.AddToScheme(client.Scheme()); err != nil {
return nil, err
}

dc := dynamic.NewForConfigOrDie(restConfig)

Expand All @@ -66,10 +73,18 @@ func NewK8sClients(kubeconfig string) (*K8sClients, error) {

func MustClientsForTest(t *testing.T, initRuntimeObjects ...runtime.Object) *K8sClients {
scheme := scheme.Scheme
gatewayv1alpha2.AddToScheme(scheme)
gatewayv1beta1.AddToScheme(scheme)
gatewayv1.AddToScheme(scheme)
apiextensionsv1.AddToScheme(scheme)
if err := gatewayv1alpha2.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := gatewayv1beta1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := gatewayv1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
t.Fatal(err)
}

fakeClient := fakeclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initRuntimeObjects...).Build()
fakeDC := fakedynamicclient.NewSimpleDynamicClient(scheme, initRuntimeObjects...)
Expand Down
5 changes: 4 additions & 1 deletion gwctl/pkg/policymanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"

gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

Expand Down Expand Up @@ -67,7 +68,9 @@ func (p *PolicyManager) Init(ctx context.Context) error {
return err
}
for _, unstrucutredPolicy := range allPolicies {
p.AddPolicy(unstrucutredPolicy)
if err := p.AddPolicy(unstrucutredPolicy); err != nil {
return err
}
}

return nil
Expand Down
1 change: 0 additions & 1 deletion gwctl/pkg/policymanager/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func orderPolicyByPrecedence(a, b Policy) (Policy, Policy) {
if higherNN > lowerNN {
higherPolicy, lowerPolicy = lowerPolicy, higherPolicy
}

} else if higherPolicy.u.GetCreationTimestamp().Time.After(lowerPolicy.u.GetCreationTimestamp().Time) {
// Policies have difference creation time, so this will decide the precedence
higherPolicy, lowerPolicy = lowerPolicy, higherPolicy
Expand Down
25 changes: 16 additions & 9 deletions gwctl/pkg/printer/gatewayclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ package printer
import (
"fmt"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"os"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"sort"
"strings"
"text/tabwriter"

"sigs.k8s.io/yaml"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/yaml"
)

type GatewayClassesPrinter struct {
Expand All @@ -54,14 +53,18 @@ type gatewayClassDescribeView struct {
// GatewayClass description
Description *string `json:",omitempty"`

Status *v1.GatewayClassStatus `json:",omitempty"`
DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"`
Status *gatewayv1.GatewayClassStatus `json:",omitempty"`
DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"`
}

func (gcp *GatewayClassesPrinter) Print(model *resourcediscovery.ResourceModel) {
tw := tabwriter.NewWriter(gcp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "CONTROLLER", "ACCEPTED", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

gatewayClassNodes := make([]*resourcediscovery.GatewayClassNode, 0, len(model.GatewayClasses))
for _, gatewayClassNode := range model.GatewayClasses {
Expand Down Expand Up @@ -91,7 +94,11 @@ func (gcp *GatewayClassesPrinter) Print(model *resourcediscovery.ResourceModel)
accepted,
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand Down
14 changes: 11 additions & 3 deletions gwctl/pkg/printer/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
"sigs.k8s.io/yaml"

"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
"sigs.k8s.io/yaml"
)

type GatewaysPrinter struct {
Expand All @@ -50,7 +50,11 @@ type gatewayDescribeView struct {
func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel) {
tw := tabwriter.NewWriter(gp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "CLASS", "ADDRESSES", "PORTS", "PROGRAMMED", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

gatewayNodes := make([]*resourcediscovery.GatewayNode, 0, len(resourceModel.Gateways))
for _, gatewayNode := range resourceModel.Gateways {
Expand Down Expand Up @@ -98,7 +102,11 @@ func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel)
programmedStatus,
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand Down
14 changes: 11 additions & 3 deletions gwctl/pkg/printer/httproutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
"sigs.k8s.io/yaml"

"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
"sigs.k8s.io/yaml"
)

type HTTPRoutesPrinter struct {
Expand All @@ -41,7 +41,11 @@ type HTTPRoutesPrinter struct {
func (hp *HTTPRoutesPrinter) Print(resourceModel *resourcediscovery.ResourceModel) {
tw := tabwriter.NewWriter(hp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAMESPACE", "NAME", "HOSTNAMES", "PARENT REFS", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

httpRouteNodes := make([]*resourcediscovery.HTTPRouteNode, 0, len(resourceModel.HTTPRoutes))
for _, httpRouteNode := range resourceModel.HTTPRoutes {
Expand Down Expand Up @@ -80,7 +84,11 @@ func (hp *HTTPRoutesPrinter) Print(resourceModel *resourcediscovery.ResourceMode
parentRefsCount,
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand Down
14 changes: 11 additions & 3 deletions gwctl/pkg/printer/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
"sigs.k8s.io/yaml"

"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
"sigs.k8s.io/yaml"
)

type NamespacesPrinter struct {
Expand All @@ -48,7 +48,11 @@ type namespaceDescribeView struct {
func (nsp *NamespacesPrinter) Print(resourceModel *resourcediscovery.ResourceModel) {
tw := tabwriter.NewWriter(nsp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "STATUS", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

namespaceNodes := make([]*resourcediscovery.NamespaceNode, 0, len(resourceModel.Namespaces))
for _, namespaceNode := range resourceModel.Namespaces {
Expand All @@ -66,7 +70,11 @@ func (nsp *NamespacesPrinter) Print(resourceModel *resourcediscovery.ResourceMod
string(namespaceNode.Namespace.Status.Phase),
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand Down
26 changes: 21 additions & 5 deletions gwctl/pkg/printer/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"text/tabwriter"

"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/yaml"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
"sigs.k8s.io/yaml"
)

type PoliciesPrinter struct {
Expand All @@ -47,7 +47,11 @@ func (pp *PoliciesPrinter) PrintPoliciesGetView(policies []policymanager.Policy)

tw := tabwriter.NewWriter(pp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "KIND", "TARGET NAME", "TARGET KIND", "POLICY TYPE", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

for _, policy := range policies {
policyType := "Direct"
Expand All @@ -67,7 +71,11 @@ func (pp *PoliciesPrinter) PrintPoliciesGetView(policies []policymanager.Policy)
policyType,
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand All @@ -81,7 +89,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsGetView(policyCRDs []policymanager.Pol

tw := tabwriter.NewWriter(pp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "POLICY TYPE", "SCOPE", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}

for _, policyCRD := range policyCRDs {
policyType := "Direct"
Expand All @@ -97,7 +109,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsGetView(policyCRDs []policymanager.Pol
string(policyCRD.CRD().Spec.Scope),
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
_, err := tw.Write([]byte(strings.Join(row, "\t") + "\n"))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
tw.Flush()
}
Expand Down
Loading

0 comments on commit 12b3379

Please sign in to comment.