Skip to content

Commit

Permalink
resolve golang linter issues, gofmt category
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffhuang4704 committed Oct 13, 2024
1 parent c99f4dd commit 4bd9113
Show file tree
Hide file tree
Showing 34 changed files with 226 additions and 229 deletions.
2 changes: 1 addition & 1 deletion controller/access/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func GetUserPermissions(role string, roleDomains map[string][]string, extraPermi

// 2-3. merge #2-1 & #2-2 to get the top-level permissions list for each domain
dPermitsList := make(map[string][]*api.RESTRolePermission, len(allDomains)) // domain -> list of permissions
for domain, _ := range allDomains {
for domain := range allDomains {
roles, _ := domainRoles[domain]
extraPermits, _ := domainPermits[domain]
if permitsList := getDomainPermissions(domain, roles, extraPermits); len(permitsList) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion controller/api/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (o *Event) GetDomain(f share.GetAccessObjectFunc) ([]string, []string) {
return []string{o.WorkloadDomain}, nil
} else if o.UserRoles != nil {
list := make([]string, 0, len(o.UserRoles))
for domain, _ := range o.UserRoles {
for domain := range o.UserRoles {
list = append(list, domain)
}
return list, nil
Expand Down
30 changes: 15 additions & 15 deletions controller/api/internal_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ type RESTRiskScoreMetricsWL struct {
}

type RESTRiskScoreMetricsGroup struct {
Groups int `json:"groups"`
DiscoverGroups int `json:"discover_groups"`
MonitorGroups int `json:"monitor_groups"`
ProtectGroups int `json:"protect_groups"`
ProfileDiscoverGroups int `json:"profile_discover_groups"`
ProfileMonitorGroups int `json:"profile_monitor_groups"`
ProfileProtectGroups int `json:"profile_protect_groups"`
DiscoverGroupsZD int `json:"discover_groups_zero_drift"`
MonitorGroupsZD int `json:"monitor_groups_zero_drift"`
ProtectGroupsZD int `json:"protect_groups_zero_drift"`
Groups int `json:"groups"`
DiscoverGroups int `json:"discover_groups"`
MonitorGroups int `json:"monitor_groups"`
ProtectGroups int `json:"protect_groups"`
ProfileDiscoverGroups int `json:"profile_discover_groups"`
ProfileMonitorGroups int `json:"profile_monitor_groups"`
ProfileProtectGroups int `json:"profile_protect_groups"`
DiscoverGroupsZD int `json:"discover_groups_zero_drift"`
MonitorGroupsZD int `json:"monitor_groups_zero_drift"`
ProtectGroupsZD int `json:"protect_groups_zero_drift"`
}

type RESTRiskScoreMetricsCVE struct {
Expand Down Expand Up @@ -229,9 +229,9 @@ type RESTK8sNvAcceptableAlerts struct {
}

type RESTNvAlerts struct {
NvUpgradeInfo *RESTCheckUpgradeInfo `json:"neuvector_upgrade_info"`
AcceptableAlerts *RESTNvAcceptableAlerts `json:"acceptable_alerts,omitempty"` // acceptable controller-generated alerts
AcceptedAlerts []string `json:"accepted_alerts,omitempty"` // keys of accepted manager-generated/user alerts
NvUpgradeInfo *RESTCheckUpgradeInfo `json:"neuvector_upgrade_info"`
AcceptableAlerts *RESTNvAcceptableAlerts `json:"acceptable_alerts,omitempty"` // acceptable controller-generated alerts
AcceptedAlerts []string `json:"accepted_alerts,omitempty"` // keys of accepted manager-generated/user alerts
}

type RESTNvAcceptableAlerts struct {
Expand All @@ -257,8 +257,8 @@ type RESTNvAlertGroup struct {
}

type RESTNvAlert struct {
ID string `json:"id"` // ID is md5 of the English message
Message string `json:"message"`
ID string `json:"id"` // ID is md5 of the English message
Message string `json:"message"`
}

type RESTAcceptedAlerts struct {
Expand Down
117 changes: 58 additions & 59 deletions controller/atmo/atmo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package atmo

import (
"time"
"strings"
"sync"
"time"

log "github.com/sirupsen/logrus"

Expand All @@ -16,13 +16,13 @@ const (
)

const (
AllMode = 0 // not used
AllMode = 0 // not used
ProfileMode = 1
PolicyMode = 2
)

const (
allModeType = "@all" // not used
allModeType = "@all" // not used
profileType = "@profile"
policyType = "@policy"
)
Expand All @@ -37,71 +37,70 @@ type AutoModeHelper interface {
GetTheGroupType(group string) (int, string)
}

//////
type testFunc func(mover int, group string, probeDuration time.Duration) (bool, error)
// ////
type testFunc func(mover int, group string, probeDuration time.Duration) (bool, error)
type decisionFunc func(mover int, group string, err error) error
type automode_ctx struct {
bD2M bool
bM2P bool
bD2M bool
bM2P bool

// controls
mutex sync.Mutex
timerWheel *utils.TimerWheel
mutex sync.Mutex
timerWheel *utils.TimerWheel

// data
d2m_members map[string]*task
d2m_members map[string]*task
m2p_members map[string]*task

// parameters
d2m_cmpl time.Duration // complete period
m2p_cmpl time.Duration
d2m_itl time.Duration // probe interval
m2p_itl time.Duration
d2m_life int // maximum probe counts
m2p_life int
d2m_cmpl time.Duration // complete period
m2p_cmpl time.Duration
d2m_itl time.Duration // probe interval
m2p_itl time.Duration
d2m_life int // maximum probe counts
m2p_life int

// callbacks
probefn probeFunc
probefn probeFunc

// test functions
testfn testFunc
decidefn decisionFunc
testfn testFunc
decidefn decisionFunc
}

//
var atmo_ctx *automode_ctx
var atmo_ctx *automode_ctx

const discover_probe = time.Second * 60 // interval: 1 minute
const discover_complete = time.Hour * 6 // convert into counters
const monitor_probe = time.Minute * 5 // interval: 5
const monitor_complete = time.Hour * 12 // convert into counters
const discover_probe = time.Second * 60 // interval: 1 minute
const discover_complete = time.Hour * 6 // convert into counters
const monitor_probe = time.Minute * 5 // interval: 5
const monitor_complete = time.Hour * 12 // convert into counters

///
func Init(timerWheel *utils.TimerWheel, test_cb testFunc, decision_cb decisionFunc) (*automode_ctx) {
// /
func Init(timerWheel *utils.TimerWheel, test_cb testFunc, decision_cb decisionFunc) *automode_ctx {
log.Debug("ATMO:")
atmo_ctx = &automode_ctx {
atmo_ctx = &automode_ctx{
d2m_members: make(map[string]*task),
m2p_members: make(map[string]*task),
timerWheel: timerWheel,
d2m_cmpl: discover_complete, // complete period
d2m_cmpl: discover_complete, // complete period
m2p_cmpl: monitor_complete,
d2m_itl: discover_probe, // probe interval
d2m_itl: discover_probe, // probe interval
m2p_itl: monitor_probe,
testfn: test_cb,
testfn: test_cb,
decidefn: decision_cb,
}

// prepare counters
atmo_ctx.d2m_life = (int)(atmo_ctx.d2m_cmpl/atmo_ctx.d2m_itl)
atmo_ctx.m2p_life = (int)(int64(atmo_ctx.m2p_cmpl/atmo_ctx.m2p_itl))
atmo_ctx.d2m_life = (int)(atmo_ctx.d2m_cmpl / atmo_ctx.d2m_itl)
atmo_ctx.m2p_life = (int)(int64(atmo_ctx.m2p_cmpl / atmo_ctx.m2p_itl))
return atmo_ctx
}

func GetAutoModeHelper() AutoModeHelper {
return atmo_ctx
}

///////////////////////////////////////////////////////
// /////////////////////////////////////////////////////
func (ctx *automode_ctx) lock() {
ctx.mutex.Lock()
}
Expand All @@ -110,52 +109,52 @@ func (ctx *automode_ctx) unlock() {
ctx.mutex.Unlock()
}

////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
// internal testing purpose: do before the first Configure() function
func (ctx *automode_ctx) ConfigProbeTime(mover int, probe time.Duration) {
log.WithFields(log.Fields{"probe": probe, "mover": mover}).Info("ATMO:")
switch mover {
case Discover2Monitor:
ctx.d2m_itl = probe
ctx.d2m_life = (int)(ctx.d2m_cmpl/ctx.d2m_itl)
ctx.d2m_life = (int)(ctx.d2m_cmpl / ctx.d2m_itl)
case Monitor2Protect:
ctx.m2p_itl = probe
ctx.m2p_life = (int)(int64(ctx.m2p_cmpl/ctx.m2p_itl))
ctx.m2p_life = (int)(int64(ctx.m2p_cmpl / ctx.m2p_itl))
default:
log.WithFields(log.Fields{"probe": probe, "mover": mover}).Error("ATMO:")
}
}

func (ctx *automode_ctx) GetTheGroupType(group string) (int, string) {
if strings.HasSuffix(group, profileType) {
return ProfileMode, strings.TrimSuffix(group, profileType) // profile
return ProfileMode, strings.TrimSuffix(group, profileType) // profile
}
return PolicyMode, strings.TrimSuffix(group, policyType) // policy
}

//////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
func (ctx *automode_ctx) ConfigureCompleteDuration(mover int, dComplete time.Duration) {
log.WithFields(log.Fields{"complete": dComplete, "mover": mover}).Debug("ATMO:")
switch mover {
case Discover2Monitor:
if dComplete == 0 { // disabled
if dComplete == 0 { // disabled
ctx.pruneMembers(Discover2Monitor)
ctx.bD2M = false
} else {
if ctx.d2m_cmpl != dComplete {
ctx.d2m_cmpl = dComplete
ctx.d2m_life = (int)(ctx.d2m_cmpl/ctx.d2m_itl)
ctx.d2m_life = (int)(ctx.d2m_cmpl / ctx.d2m_itl)
}
ctx.bD2M = true
}
case Monitor2Protect:
if dComplete == 0 { // disabled
if dComplete == 0 { // disabled
ctx.pruneMembers(Monitor2Protect)
ctx.bM2P = false
} else {
if ctx.m2p_cmpl != dComplete {
ctx.m2p_cmpl = dComplete
ctx.m2p_life = (int)(int64(ctx.m2p_cmpl/ctx.m2p_itl))
ctx.m2p_life = (int)(int64(ctx.m2p_cmpl / ctx.m2p_itl))
}
ctx.bM2P = true
}
Expand All @@ -174,18 +173,18 @@ func (ctx *automode_ctx) AddGroup(mover int, theGroup string, modeType int) bool
}

switch mover {
case Discover2Monitor:
ctx.removeMember(Monitor2Protect, group)
if !ctx.bD2M {
return false
}
case Monitor2Protect:
ctx.removeMember(Discover2Monitor, group)
if !ctx.bM2P {
return false
}
default:
case Discover2Monitor:
ctx.removeMember(Monitor2Protect, group)
if !ctx.bD2M {
return false
}
case Monitor2Protect:
ctx.removeMember(Discover2Monitor, group)
if !ctx.bM2P {
return false
}
default:
return false
}
return ctx.addMember(mover, group)
}
Expand All @@ -194,10 +193,10 @@ func (ctx *automode_ctx) RemoveGroup(group string) {
// log.WithFields(log.Fields{"group": group}).Debug("ATMO:")

// where is it?
ctx.removeMember(Discover2Monitor, group + profileType)
ctx.removeMember(Monitor2Protect, group + profileType)
ctx.removeMember(Discover2Monitor, group + policyType)
ctx.removeMember(Monitor2Protect, group + policyType)
ctx.removeMember(Discover2Monitor, group+profileType)
ctx.removeMember(Monitor2Protect, group+profileType)
ctx.removeMember(Discover2Monitor, group+policyType)
ctx.removeMember(Monitor2Protect, group+policyType)
}

func (ctx *automode_ctx) Counts(mover int) int {
Expand Down Expand Up @@ -227,7 +226,7 @@ func (ctx *automode_ctx) List(mover int) []string {
return list
}

for n, _ := range members {
for n := range members {
list = append(list, n)
}
return list
Expand Down
42 changes: 21 additions & 21 deletions controller/atmo/atmo_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"github.com/neuvector/neuvector/share/utils"
)

////////////////////
// //////////////////
type probeFunc func(mover int, id string, probeDuration time.Duration) (bool, error)
type lifeCntFunc func(mover int) int
type completeFunc func(mover int, group string, err error) error
type task struct {
mover int
id string // group name
timer string
timerWheel *utils.TimerWheel
runs int // good counter
interval time.Duration // probe interval
testFunc probeFunc
lifeFunc lifeCntFunc
cmplFunc completeFunc
mover int
id string // group name
timer string
timerWheel *utils.TimerWheel
runs int // good counter
interval time.Duration // probe interval
testFunc probeFunc
lifeFunc lifeCntFunc
cmplFunc completeFunc
}

///////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////
func (t *task) StartTimer() {
t.timer, _ = t.timerWheel.AddTask(t, t.interval)
}
Expand Down Expand Up @@ -52,7 +52,7 @@ func (t *task) Expire() {
} else {
//log.WithFields(log.Fields{"group": t.id}).Debug("ATMO:")
if t.mover == Monitor2Protect {
t.runs = 0 // reset counters
t.runs = 0 // reset counters
}
}

Expand All @@ -66,7 +66,7 @@ func (t *task) Expire() {
}
}

///////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////
func (ctx *automode_ctx) prober(mover int, group string, dur time.Duration) (bool, error) {
return ctx.testfn(mover, group, dur)
}
Expand Down Expand Up @@ -102,24 +102,24 @@ func (ctx *automode_ctx) addMember(mover int, group string) bool {
switch mover {
case Discover2Monitor:
members = ctx.d2m_members
interval = ctx.d2m_itl
interval = ctx.d2m_itl
case Monitor2Protect:
members = ctx.m2p_members
interval = ctx.m2p_itl
interval = ctx.m2p_itl
default:
return false
}

if _, ok := members[group]; !ok {
log.WithFields(log.Fields{"group": group, "mover": mover}).Debug("ATMO:")
t := &task {
id: group,
mover: mover,
interval: interval,
t := &task{
id: group,
mover: mover,
interval: interval,
timerWheel: ctx.timerWheel,
testFunc: ctx.prober,
testFunc: ctx.prober,
lifeFunc: ctx.life,
cmplFunc: ctx.finisher,
cmplFunc: ctx.finisher,
}
members[group] = t
t.StartTimer()
Expand Down
Loading

0 comments on commit 4bd9113

Please sign in to comment.