Skip to content

Commit

Permalink
Fix CI linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
STARRY-S committed Jul 11, 2024
1 parent 240352f commit e0c0538
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 64 deletions.
22 changes: 0 additions & 22 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@ updates:
ignore:
# Ignore k8s and its transitives modules as they are upgraded manually
# together with controller-runtime.
- dependency-name: "k8s.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch" ]
- dependency-name: "go.etcd.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
# Ignore controller-runtime as its upgraded manually.
- dependency-name: "sigs.k8s.io/controller-runtime"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
# Ignore k8s and its transitives modules as they are upgraded manually
# together with controller-runtime.
- dependency-name: "k8s.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
- dependency-name: "go.etcd.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
# Ignore wrangler
- dependency-name: "github.com/rancher/wrangler"
update-types: [ "version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch" ]
# Ignore wrangler v3
Expand All @@ -52,12 +42,6 @@ updates:
kubernetes:
patterns: [ "k8s.io/*" ]
ignore:
# Ignore k8s and its transitives modules as they are upgraded manually
# together with controller-runtime.
- dependency-name: "k8s.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch" ]
- dependency-name: "go.etcd.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
# Ignore controller-runtime as its upgraded manually.
- dependency-name: "sigs.k8s.io/controller-runtime"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
Expand Down Expand Up @@ -87,12 +71,6 @@ updates:
kubernetes:
patterns: [ "k8s.io/*" ]
ignore:
# Ignore k8s and its transitives modules as they are upgraded manually
# together with controller-runtime.
- dependency-name: "k8s.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch" ]
- dependency-name: "go.etcd.io/*"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
# Ignore controller-runtime as its upgraded manually.
- dependency-name: "sigs.k8s.io/controller-runtime"
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
run:
timeout: 5m
go: "1.19"
go: "1.22"
skip-files:
- "zz_generated_*"
tests: false
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/cce.pandaria.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ type CCEClusterExtendParam struct {
}

type CCEClusterEndpoints struct {
Url string `json:"url,omitempty"`
URL string `json:"url,omitempty"`
Type string `json:"type,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newCRD(obj interface{}, customize func(crd.CRD) crd.CRD) crd.CRD {

func saveCRDYaml(name, data string) error {
filename := fmt.Sprintf("./charts/%s/templates/crds.yaml", name)
if err := os.WriteFile(filename, []byte(data), 0644); err != nil {
if err := os.WriteFile(filename, []byte(data), 0600); err != nil {
return err
}

Expand Down
25 changes: 12 additions & 13 deletions pkg/controller/cce-cluster-config-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (h *Handler) recordError(
if err != nil {
logrus.Warnf("%v", err)
if huawei.IsHuaweiError(err) {
hwerr, _ := huawei.NewHuaweiError(err)
hwerr, _ := huawei.NewError(err)
hwerr.RequestID = ""
message = hwerr.String()
} else {
Expand Down Expand Up @@ -298,7 +298,7 @@ func (h *Handler) generateAndSetNetworking(config *ccev1.CCEClusterConfig) (*cce
if dnsServers.Nameservers == nil || len(*dnsServers.Nameservers) == 0 {
return config, fmt.Errorf("ListNameServers returns invalid data")
}
var dnsRecords []string = make([]string, 2)
var dnsRecords = make([]string, 2)
for _, nameserver := range *dnsServers.Nameservers {
if nameserver.NsRecords == nil || len(*nameserver.NsRecords) == 0 {
continue
Expand Down Expand Up @@ -387,7 +387,7 @@ func (h *Handler) generateAndSetNetworking(config *ccev1.CCEClusterConfig) (*cce
if dnsServers.Nameservers == nil || len(*dnsServers.Nameservers) == 0 {
return config, fmt.Errorf("ListNameServers returns invalid data")
}
var dnsRecords []string = make([]string, 2)
var dnsRecords = make([]string, 2)
for _, nameserver := range *dnsServers.Nameservers {
if nameserver.NsRecords == nil || len(*nameserver.NsRecords) == 0 {
continue
Expand Down Expand Up @@ -656,14 +656,13 @@ func (h *Handler) checkAndUpdate(config *ccev1.CCEClusterConfig) (*ccev1.CCEClus

res, err := cce.ShowUpgradeClusterTask(driver.CCE, config.Spec.ClusterID, config.Status.UpgradeClusterTaskID)
if err != nil {
hwerr, _ := huawei.NewHuaweiError(err)
hwerr, _ := huawei.NewError(err)
if hwerr.StatusCode == 404 {
config = config.DeepCopy()
config.Status.UpgradeClusterTaskID = ""
return h.cceCC.UpdateStatus(config)
} else {
return config, err
}
return config, err
}
if res != nil && res.Spec != nil && res.Status != nil {
switch utils.Value(res.Status.Phase) {
Expand Down Expand Up @@ -722,7 +721,7 @@ func (h *Handler) checkAndUpdate(config *ccev1.CCEClusterConfig) (*ccev1.CCEClus
if len(configUpdate.Status.Endpoints) == len(*cluster.Status.Endpoints) {
for i := range *cluster.Status.Endpoints {
if configUpdate.Status.Endpoints[i].Type != utils.Value((*cluster.Status.Endpoints)[i].Type) ||
configUpdate.Status.Endpoints[i].Url != utils.Value((*cluster.Status.Endpoints)[i].Url) {
configUpdate.Status.Endpoints[i].URL != utils.Value((*cluster.Status.Endpoints)[i].Url) {
updateEndpoints = true
}
}
Expand All @@ -734,7 +733,7 @@ func (h *Handler) checkAndUpdate(config *ccev1.CCEClusterConfig) (*ccev1.CCEClus
configUpdate.Status.Endpoints = nil
for _, e := range *cluster.Status.Endpoints {
configUpdate.Status.Endpoints = append(configUpdate.Status.Endpoints, ccev1.CCEClusterEndpoints{
Url: utils.Value(e.Url),
URL: utils.Value(e.Url),
Type: utils.Value(e.Type),
})
}
Expand Down Expand Up @@ -913,7 +912,7 @@ func (h *Handler) updateUpstreamClusterState(
if np.ID == "" {
continue
}
_, err := cce.UpdateNodePool(driver.CCE, config.Spec.ClusterID, &np)
_, err := cce.UpdateNodePool(driver.CCE, config.Spec.ClusterID, np.DeepCopy())
if err != nil {
return config, err
}
Expand Down Expand Up @@ -1133,17 +1132,17 @@ func (h *Handler) createCASecret(config *ccev1.CCEClusterConfig) error {
return fmt.Errorf("createCASecret failed: no clusters returned from GetClusterCert")
}

var clusterCert *cce_model.Clusters
var clusterCert cce_model.Clusters
for _, c := range *certs.Clusters {
if config.Spec.PublicAccess && utils.Value(c.Name) == "externalClusterTLSVerify" {
clusterCert = &c
clusterCert = c
break
}
if utils.Value(c.Name) == "internalCluster" {
clusterCert = &c
clusterCert = c
}
}
if clusterCert == nil {
if clusterCert.Name == nil {
return fmt.Errorf("createCASecret: failed to find cluster endpoint")
}
if clusterCert.Cluster == nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *Handler) deleteCCECluster(
}

cluster, err := cce.ShowCluster(driver.CCE, config.Spec.ClusterID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
// Cluster deleted, update status.
logrus.WithFields(logrus.Fields{
"cluster": config.Name,
Expand Down Expand Up @@ -211,7 +211,7 @@ func (h *Handler) deleteNetworkResources(

// Delete NatGateway.
_, err = nat.ShowNatGateway(driver.NAT, config.Status.CreatedNatGatewayID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
logrus.WithFields(logrus.Fields{
"cluster": config.Name,
"phase": "remove",
Expand Down Expand Up @@ -240,7 +240,7 @@ func (h *Handler) deleteNetworkResources(
if config.Status.CreatedClusterEIPID != "" {
eipID := config.Status.CreatedClusterEIPID
_, err = eip.ShowPublicip(driver.EIP, eipID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
config = config.DeepCopy()
config.Status.CreatedClusterEIPID = ""
config, err = h.cceCC.UpdateStatus(config)
Expand All @@ -264,7 +264,7 @@ func (h *Handler) deleteNetworkResources(
if config.Status.CreatedSNatRuleEIPID != "" {
eipID := config.Status.CreatedSNatRuleEIPID
_, err = eip.ShowPublicip(driver.EIP, eipID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
config = config.DeepCopy()
config.Status.CreatedSNatRuleEIPID = ""
config, err = h.cceCC.UpdateStatus(config)
Expand Down Expand Up @@ -292,7 +292,7 @@ func (h *Handler) deleteNetworkResources(
)
if subnetID != "" {
_, err = vpc.ShowSubnet(driver.VPC, subnetID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
logrus.WithFields(logrus.Fields{
"cluster": config.Name,
"phase": "remove",
Expand Down Expand Up @@ -343,7 +343,7 @@ func (h *Handler) deleteNetworkResources(
return config, true, nil
}
_, err = vpc.ShowVPC(driver.VPC, vpcID)
if hwerr, _ := huawei.NewHuaweiError(err); hwerr.StatusCode == 404 {
if hwerr, _ := huawei.NewError(err); hwerr.StatusCode == 404 {
logrus.WithFields(logrus.Fields{
"cluster": config.Name,
"phase": "remove",
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewHuaweiDriver(auth *common.ClientAuth) *HuaweiDriver {
VPC: vpc.NewVpcClient(auth),
EIP: eip.NewEipClient(auth),
VPCEP: vpcep.NewVpcepClient(auth),
DNS: dns.NewDnsClient(auth),
DNS: dns.NewDNSClient(auth),
NAT: nat.NewNatClient(auth),
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func BuildUpstreamNodePoolConfigs(
if nodePools == nil || nodePools.Items == nil {
return nil, fmt.Errorf("BuildUpstreamNodePoolConfigs: invalid nil parameter")
}
var nps []ccev1.CCENodePool = make([]ccev1.CCENodePool, 0, len(*nodePools.Items))
nps := make([]ccev1.CCENodePool, 0, len(*nodePools.Items))
if len(*nodePools.Items) == 0 {
return nps, nil
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func validateNodePool(config *ccev1.CCEClusterConfig) error {
}
if nodePoolNames[pool.Name] {
return fmt.Errorf("nodePool.name should be unique, duplicated detected: %q", pool.Name)
} else {
nodePoolNames[pool.Name] = true
}
nodePoolNames[pool.Name] = true
nt := pool.NodeTemplate
if nt.Flavor == "" {
return fmt.Errorf(cannotBeEmptyError, "nodePool.nodeTemplate.flavor", config.Name)
Expand Down Expand Up @@ -83,7 +82,7 @@ func (h *Handler) validateCreate(config *ccev1.CCEClusterConfig) error {
}
_, err := cce.ShowCluster(driver.CCE, config.Spec.ClusterID)
if err != nil {
hwerr, _ := huawei.NewHuaweiError(err)
hwerr, _ := huawei.NewError(err)
if hwerr.StatusCode == 404 {
return fmt.Errorf("failed to find cluster [%s]: %v",
config.Spec.ClusterID, hwerr.ErrorMessage)
Expand Down
2 changes: 1 addition & 1 deletion pkg/huawei/cce/cce.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func GetCreateClusterRequest(config *ccev1.CCEClusterConfig) *model.CreateCluste
kubeProxyMode = model.GetClusterSpecKubeProxyModeEnum().IPTABLES
}

var clusterTags []model.ResourceTag
clusterTags := []model.ResourceTag{}
for k, v := range spec.Tags {
clusterTags = append(clusterTags, model.ResourceTag{
Key: utils.Pointer(k),
Expand Down
2 changes: 1 addition & 1 deletion pkg/huawei/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/sirupsen/logrus"
)

func NewDnsClient(c *common.ClientAuth) *dns.DnsClient {
func NewDNSClient(c *common.ClientAuth) *dns.DnsClient {
return dns.NewDnsClient(
dns.DnsClientBuilder().
WithRegion(region.ValueOf(c.Region)).
Expand Down
12 changes: 6 additions & 6 deletions pkg/huawei/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ import (
"encoding/json"
)

type HuaweiError struct {
type Error struct {
StatusCode int32 `json:"status_code,omitempty"`
RequestID string `json:"request_id,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}

func NewHuaweiError(err error) (*HuaweiError, error) {
func NewError(err error) (*Error, error) {
var errMsg string
if err != nil {
errMsg = err.Error()
}
d := json.NewDecoder(bytes.NewBufferString(errMsg))
huaweiError := &HuaweiError{}
huaweiError := &Error{}
err = d.Decode(huaweiError)
return huaweiError, err
}

func IsHuaweiError(err error) bool {
d := json.NewDecoder(bytes.NewBufferString(err.Error()))
h := &HuaweiError{}
h := &Error{}
if err := d.Decode(h); err != nil {
return false
}
return true
}

func (e *HuaweiError) String() string {
func (e *Error) String() string {
d, _ := json.Marshal(e)
return string(d)
}

func (e *HuaweiError) MarshalIndent() string {
func (e *Error) MarshalIndent() string {
d, _ := json.MarshalIndent(e, "", " ")
return string(d)
}
6 changes: 3 additions & 3 deletions pkg/huawei/huawei_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ func Test_IsHuaweiError(t *testing.T) {

func Test_NewHuaweiError(t *testing.T) {
assert := assert.New(t)
vpcErr, err := NewHuaweiError(fmt.Errorf(vpcDeleteError))
vpcErr, err := NewError(fmt.Errorf(vpcDeleteError))
assert.Nil(err)
if t.Failed() {
return
}
fmt.Printf("%v\n", vpcErr.MarshalIndent())
fmt.Printf("%v\n", vpcErr.String())
clusterError, err := NewHuaweiError(fmt.Errorf(clusterDeleteError))
clusterError, err := NewError(fmt.Errorf(clusterDeleteError))
assert.Nil(err)
if t.Failed() {
return
}
fmt.Printf("%v\n", clusterError.MarshalIndent())
fmt.Printf("%v\n", clusterError.String())
subnetError, err := NewHuaweiError(fmt.Errorf(subnetDeleteError))
subnetError, err := NewError(fmt.Errorf(subnetDeleteError))
assert.Nil(err)
if t.Failed() {
return
Expand Down
12 changes: 8 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package utils

import (
"crypto/rand"
"encoding/json"
"math/rand"
"math/big"
"strings"
"time"
)

func PrintObject(a any) string {
Expand All @@ -22,11 +22,15 @@ func Parse(ref string) (namespace string, name string) {

// Generates a random hexadecimal number.
func RandomHex(l int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
chars := []byte("abcdef0123456789")
big := new(big.Int).SetInt64(int64(len(chars)))
var b strings.Builder
for i := 0; i < l; i++ {
b.WriteByte(chars[r.Intn(len(chars))])
i, err := rand.Int(rand.Reader, big)
if err != nil {
return ""
}
b.WriteByte(chars[i.Int64()])
}

return b.String()
Expand Down

0 comments on commit e0c0538

Please sign in to comment.