Skip to content

Commit aa0c220

Browse files
authored
Merge pull request #214 from flanksource/config-class-rename
feat: rename config_type to config_class
2 parents d8cc7e0 + ca923ac commit aa0c220

32 files changed

+439
-482
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
4040
resources: fmt manifests
4141

4242
test: manifests generate fmt vet envtest ## Run tests.
43-
go test ./... -coverprofile cover.out
4443
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
4544

4645
fmt:

api/v1/interface.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
// +kubebuilder:object:generate=false
5454
type AnalysisResult struct {
5555
ExternalID string
56-
ExternalType string
56+
ConfigType string
5757
Summary string // Summary of the analysis
5858
Analysis map[string]any // Detailed metadata of the analysis
5959
AnalysisType AnalysisType // Type of analysis, e.g. availability, compliance, cost, security, performance.
@@ -72,7 +72,7 @@ type AnalysisResult struct {
7272
func (t *AnalysisResult) ToConfigAnalysis() models.ConfigAnalysis {
7373
return models.ConfigAnalysis{
7474
ExternalID: t.ExternalID,
75-
ExternalType: t.ExternalType,
75+
ConfigType: t.ConfigType,
7676
Analyzer: t.Analyzer,
7777
Message: strings.Join(t.Messages, ";"),
7878
Severity: string(t.Severity),
@@ -89,7 +89,7 @@ func (t *AnalysisResult) ToConfigAnalysis() models.ConfigAnalysis {
8989
// +kubebuilder:object:generate=false
9090
type ChangeResult struct {
9191
ExternalID string
92-
ExternalType string
92+
ConfigType string
9393
ExternalChangeID string
9494
Action ChangeAction
9595
ChangeType string
@@ -102,7 +102,7 @@ type ChangeResult struct {
102102
}
103103

104104
func (c ChangeResult) String() string {
105-
return fmt.Sprintf("%s/%s: %s", c.ExternalType, c.ExternalID, c.ChangeType)
105+
return fmt.Sprintf("%s/%s: %s", c.ConfigType, c.ExternalID, c.ChangeType)
106106
}
107107

108108
func (result AnalysisResult) String() string {
@@ -163,11 +163,11 @@ func (s *ScrapeResults) AddChange(change ChangeResult) *ScrapeResults {
163163
return s
164164
}
165165

166-
func (s *ScrapeResults) Analysis(analyzer string, externalType string, id string) *AnalysisResult {
166+
func (s *ScrapeResults) Analysis(analyzer string, configType string, id string) *AnalysisResult {
167167
result := AnalysisResult{
168-
Analyzer: analyzer,
169-
ExternalType: externalType,
170-
ExternalID: id,
168+
Analyzer: analyzer,
169+
ConfigType: configType,
170+
ExternalID: id,
171171
}
172172
*s = append(*s, ScrapeResult{
173173
AnalysisResult: &result,
@@ -187,13 +187,8 @@ type ScrapeResult struct {
187187
CreatedAt *time.Time `json:"created_at,omitempty"`
188188
DeletedAt *time.Time `json:"deleted_at,omitempty"`
189189
LastModified time.Time `json:"last_modified,omitempty"`
190-
Type string `json:"type,omitempty"`
191-
ExternalType string `json:"external_type,omitempty"`
192-
Account string `json:"account,omitempty"`
193-
Network string `json:"network,omitempty"`
194-
Subnet string `json:"subnet,omitempty"`
195-
Region string `json:"region,omitempty"`
196-
Zone string `json:"zone,omitempty"`
190+
ConfigClass string `json:"config_class,omitempty"`
191+
Type string `json:"config_type,omitempty"`
197192
Name string `json:"name,omitempty"`
198193
Namespace string `json:"namespace,omitempty"`
199194
ID string `json:"id,omitempty"`
@@ -211,7 +206,7 @@ type ScrapeResult struct {
211206
Ignore []string `json:"-"`
212207
Action string `json:",omitempty"`
213208
ParentExternalID string `json:"-"`
214-
ParentExternalType string `json:"-"`
209+
ParentType string `json:"-"`
215210
}
216211

217212
func NewScrapeResult(base BaseScraper) *ScrapeResult {
@@ -236,12 +231,7 @@ func (s ScrapeResult) Clone(config interface{}) ScrapeResult {
236231
clone := ScrapeResult{
237232
LastModified: s.LastModified,
238233
Aliases: s.Aliases,
239-
Type: s.Type,
240-
Account: s.Account,
241-
Network: s.Network,
242-
Subnet: s.Subnet,
243-
Region: s.Region,
244-
Zone: s.Zone,
234+
ConfigClass: s.ConfigClass,
245235
Name: s.Name,
246236
Namespace: s.Namespace,
247237
ID: s.ID,
@@ -256,7 +246,7 @@ func (s ScrapeResult) Clone(config interface{}) ScrapeResult {
256246
}
257247

258248
func (r ScrapeResult) String() string {
259-
s := fmt.Sprintf("%s/%s (%s)", r.Type, r.Name, r.ID)
249+
s := fmt.Sprintf("%s/%s (%s)", r.ConfigClass, r.Name, r.ID)
260250

261251
if len(r.Changes) > 0 {
262252
s += fmt.Sprintf(" changes=%d", len(r.Changes))

api/v1/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ func (c ConfigScraper) IsDebug() bool {
4747
}
4848

4949
type ExternalID struct {
50-
ExternalType string
51-
ExternalID []string
50+
ConfigType string
51+
ExternalID []string
5252
}
5353

5454
func (e ExternalID) String() string {
55-
return fmt.Sprintf("%s/%s", e.ExternalType, strings.Join(e.ExternalID, ","))
55+
return fmt.Sprintf("%s/%s", e.ConfigType, strings.Join(e.ExternalID, ","))
5656
}
5757

5858
func (e ExternalID) IsEmpty() bool {
59-
return e.ExternalType == "" && len(e.ExternalID) == 0
59+
return e.ConfigType == "" && len(e.ExternalID) == 0
6060
}
6161

6262
func (e ExternalID) CacheKey() string {
63-
return fmt.Sprintf("external_id:%s:%s", e.ExternalType, strings.Join(e.ExternalID, ","))
63+
return fmt.Sprintf("external_id:%s:%s", e.ConfigType, strings.Join(e.ExternalID, ","))
6464
}
6565

6666
func (e ExternalID) WhereClause(db *gorm.DB) *gorm.DB {
67-
return db.Where("external_type = ? AND external_id @> ?", e.ExternalType, pq.StringArray(e.ExternalID))
67+
return db.Where("type = ? AND external_id @> ?", e.ConfigType, pq.StringArray(e.ExternalID))
6868
}

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var Analyze = &cobra.Command{
3636
logger.Fatalf("Could not unmarshall %s: %v", path, err)
3737
}
3838

39-
if obj.Type == "EC2Instance" {
39+
if obj.ConfigClass == "EC2Instance" {
4040
nested, _ := json.Marshal(obj.Config)
4141
instance := aws.Instance{}
4242
if err := json.Unmarshal(nested, &instance); err != nil {

cmd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ var Run = &cobra.Command{
6060

6161
func exportResource(resource v1.ScrapeResult, filename, outputDir string) error {
6262
if resource.Config == nil && resource.AnalysisResult != nil {
63-
logger.Debugf("%s/%s => %s", resource.ExternalType, resource.ID, *resource.AnalysisResult)
63+
logger.Debugf("%s/%s => %s", resource.Type, resource.ID, *resource.AnalysisResult)
6464
return nil
6565
}
66-
outputPath := path.Join(outputDir, resource.Type, resource.Name+".json")
66+
outputPath := path.Join(outputDir, resource.ConfigClass, resource.Name+".json")
6767
_ = os.MkdirAll(path.Dir(outputPath), 0755)
6868
data, err := json.MarshalIndent(resource, "", " ")
6969
if err != nil {

db/config.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// GetConfigItem returns a single config item result
2020
func GetConfigItem(extType, extID string) (*models.ConfigItem, error) {
2121
ci := models.ConfigItem{}
22-
tx := db.Limit(1).Find(&ci, "external_type = ? and external_id @> ?", extType, pq.StringArray{extID})
22+
tx := db.Limit(1).Find(&ci, "type = ? and external_id @> ?", extType, pq.StringArray{extID})
2323
if tx.RowsAffected == 0 {
2424
return nil, nil
2525
}
@@ -59,7 +59,7 @@ func FindConfigItemID(externalID v1.ExternalID) (*string, error) {
5959

6060
func FindConfigItemFromType(configType string) ([]models.ConfigItem, error) {
6161
var ci []models.ConfigItem
62-
err := db.Find(&ci, "external_type = @type OR config_type = @type", sql.Named("type", configType)).Error
62+
err := db.Find(&ci, "type = @type OR config_class = @type", sql.Named("type", configType)).Error
6363
return ci, err
6464
}
6565

@@ -143,19 +143,14 @@ func NewConfigItemFromResult(result v1.ScrapeResult) (*models.ConfigItem, error)
143143
}
144144

145145
ci := &models.ConfigItem{
146-
ExternalID: append(result.Aliases, result.ID),
147-
ID: result.ID,
148-
ConfigType: result.Type,
149-
ExternalType: &result.ExternalType,
150-
Account: &result.Account,
151-
Region: &result.Region,
152-
Zone: &result.Zone,
153-
Network: &result.Network,
154-
Subnet: &result.Subnet,
155-
Name: &result.Name,
156-
Source: &result.Source,
157-
Tags: &result.Tags,
158-
Config: &dataStr,
146+
ExternalID: append(result.Aliases, result.ID),
147+
ID: result.ID,
148+
ConfigClass: result.ConfigClass,
149+
Type: &result.Type,
150+
Name: &result.Name,
151+
Source: &result.Source,
152+
Tags: &result.Tags,
153+
Config: &dataStr,
159154
}
160155

161156
if result.CreatedAt != nil {
@@ -166,10 +161,10 @@ func NewConfigItemFromResult(result v1.ScrapeResult) (*models.ConfigItem, error)
166161
ci.DeletedAt = result.DeletedAt
167162
}
168163

169-
if result.ParentExternalID != "" && result.ParentExternalType != "" {
164+
if result.ParentExternalID != "" && result.ParentType != "" {
170165
parentExternalID := v1.ExternalID{
171-
ExternalType: result.ParentExternalType,
172-
ExternalID: []string{result.ParentExternalID},
166+
ConfigType: result.ParentType,
167+
ExternalID: []string{result.ParentExternalID},
173168
}
174169

175170
var err error

db/models/config_change.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// ConfigChange represents the config change database table
1414
type ConfigChange struct {
1515
ExternalID string `gorm:"-"`
16-
ExternalType string `gorm:"-"`
16+
ConfigType string `gorm:"-"`
1717
ExternalChangeId string `gorm:"column:external_change_id" json:"external_change_id"`
1818
ID string `gorm:"primaryKey;unique_index;not null;column:id" json:"id"`
1919
ConfigID string `gorm:"column:config_id;default:''" json:"config_id"`
@@ -28,19 +28,19 @@ type ConfigChange struct {
2828

2929
func (c ConfigChange) GetExternalID() v1.ExternalID {
3030
return v1.ExternalID{
31-
ExternalID: []string{c.ExternalID},
32-
ExternalType: c.ExternalType,
31+
ExternalID: []string{c.ExternalID},
32+
ConfigType: c.ConfigType,
3333
}
3434
}
3535

3636
func (c ConfigChange) String() string {
37-
return fmt.Sprintf("[%s/%s] %s", c.ExternalType, c.ExternalID, c.ChangeType)
37+
return fmt.Sprintf("[%s/%s] %s", c.ConfigType, c.ExternalID, c.ChangeType)
3838
}
3939

4040
func NewConfigChangeFromV1(result v1.ScrapeResult, change v1.ChangeResult) *ConfigChange {
4141
_change := ConfigChange{
4242
ExternalID: change.ExternalID,
43-
ExternalType: change.ExternalType,
43+
ConfigType: change.ConfigType,
4444
ExternalChangeId: change.ExternalChangeID,
4545
ChangeType: change.ChangeType,
4646
Source: change.Source,
@@ -53,8 +53,8 @@ func NewConfigChangeFromV1(result v1.ScrapeResult, change v1.ChangeResult) *Conf
5353
if _change.ExternalID == "" {
5454
_change.ExternalID = result.ID
5555
}
56-
if _change.ExternalType == "" {
57-
_change.ExternalType = result.ExternalType
56+
if _change.ConfigType == "" {
57+
_change.ConfigType = result.Type
5858
}
5959

6060
return &_change

db/models/config_item.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ import (
1414
type ConfigItem struct {
1515
ID string `gorm:"primaryKey;unique_index;not null;column:id" json:"id" `
1616
ScraperID *uuid.UUID `gorm:"column:scraper_id;default:null" json:"scraper_id,omitempty"`
17-
ConfigType string `gorm:"column:config_type;default:''" json:"config_type" `
17+
ConfigClass string `gorm:"column:config_class;default:''" json:"config_class" `
1818
ExternalID pq.StringArray `gorm:"column:external_id;type:[]text" json:"external_id,omitempty" `
19-
ExternalType *string `gorm:"column:external_type;default:null" json:"external_type,omitempty" `
19+
Type *string `gorm:"column:type;default:null" json:"type,omitempty" `
2020
Name *string `gorm:"column:name;default:null" json:"name,omitempty" `
2121
Namespace *string `gorm:"column:namespace;default:null" json:"namespace,omitempty" `
2222
Description *string `gorm:"column:description;default:null" json:"description,omitempty" `
2323
Account *string `gorm:"column:account;default:null" json:"account,omitempty" `
24-
Region *string `gorm:"-" json:"region,omitempty" `
25-
Zone *string `gorm:"-" json:"zone,omitempty" `
26-
Network *string `gorm:"-" json:"network,omitempty" `
27-
Subnet *string `gorm:"-" json:"subnet,omitempty" `
2824
Config *string `gorm:"column:config;default:null" json:"config,omitempty" `
2925
Source *string `gorm:"column:source;default:null" json:"source,omitempty" `
3026
ParentID *string `gorm:"column:parent_id;default:null" json:"parent_id,omitempty"`
@@ -40,7 +36,7 @@ type ConfigItem struct {
4036
}
4137

4238
func (ci ConfigItem) String() string {
43-
return fmt.Sprintf("%s/%s", ci.ConfigType, ci.ID)
39+
return fmt.Sprintf("%s/%s", ci.ConfigClass, ci.ID)
4440
}
4541

4642
func (ci ConfigItem) ConfigJSONStringMap() (map[string]interface{}, error) {

db/update.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func deleteChangeHandler(ctx *v1.ScrapeContext, change v1.ChangeResult) error {
3131
configs := []models.ConfigItem{}
3232
tx := db.Model(&configs).
3333
Clauses(clause.Returning{Columns: []clause.Column{{Name: "id"}}}).
34-
Where("external_type = ? and external_id @> ?", change.ExternalType, pq.StringArray{change.ExternalID}).
34+
Where("type = ? and external_id @> ?", change.ConfigType, pq.StringArray{change.ExternalID}).
3535
Update("deleted_at", deletedAt)
3636

3737
if tx.Error != nil {
38-
return errors.Wrapf(tx.Error, "unable to delete config item %s/%s", change.ExternalType, change.ExternalID)
38+
return errors.Wrapf(tx.Error, "unable to delete config item %s/%s", change.ConfigType, change.ExternalID)
3939
}
4040
if tx.RowsAffected == 0 || len(configs) == 0 {
41-
logger.Warnf("Attempt to delete non-existent config item %s/%s", change.ExternalType, change.ExternalID)
41+
logger.Warnf("Attempt to delete non-existent config item %s/%s", change.ConfigType, change.ExternalID)
4242
return nil
4343
}
4444

@@ -84,7 +84,7 @@ func getParentPath(parentExternalUID v1.ExternalID) string {
8484
}
8585

8686
func updateCI(ctx *v1.ScrapeContext, ci models.ConfigItem) error {
87-
existing, err := GetConfigItem(*ci.ExternalType, ci.ID)
87+
existing, err := GetConfigItem(*ci.Type, ci.ID)
8888
if err != nil && err != gorm.ErrRecordNotFound {
8989
return errors.Wrapf(err, "unable to lookup existing config: %s", ci)
9090
}
@@ -113,7 +113,7 @@ func updateCI(ctx *v1.ScrapeContext, ci models.ConfigItem) error {
113113
if changes != nil {
114114
err := db.Create(changes).Error
115115
if nil == err {
116-
logger.Infof("[%s/%s] detected changes", ci.ConfigType, ci.ExternalID[0])
116+
logger.Infof("[%s/%s] detected changes", ci.ConfigClass, ci.ExternalID[0])
117117
} else {
118118
if IsUniqueConstraintPGErr(err) {
119119
logger.Debugf("[%s] changes not stored. Another row with the same config_id & external_change_id exists.", ci)
@@ -143,7 +143,7 @@ func updateChange(ctx *v1.ScrapeContext, result *v1.ScrapeResult) error {
143143

144144
id, err := FindConfigItemID(change.GetExternalID())
145145
if id == nil {
146-
logger.Warnf("[%s/%s] unable to find config item for change: %v", change.ExternalType, change.ExternalID, change.ChangeType)
146+
logger.Warnf("[%s/%s] unable to find config item for change: %v", change.ConfigType, change.ExternalID, change.ChangeType)
147147
return nil
148148
} else if err != nil {
149149
return err
@@ -161,15 +161,15 @@ func updateChange(ctx *v1.ScrapeContext, result *v1.ScrapeResult) error {
161161

162162
func updateAnalysis(ctx *v1.ScrapeContext, result *v1.ScrapeResult) error {
163163
analysis := result.AnalysisResult.ToConfigAnalysis()
164-
ci, err := GetConfigItem(analysis.ExternalType, analysis.ExternalID)
164+
ci, err := GetConfigItem(analysis.ConfigType, analysis.ExternalID)
165165
if ci == nil {
166-
logger.Warnf("[%s/%s] unable to find config item for analysis: %+v", analysis.ExternalType, analysis.ExternalID, analysis)
166+
logger.Warnf("[%s/%s] unable to find config item for analysis: %+v", analysis.ConfigType, analysis.ExternalID, analysis)
167167
return nil
168168
} else if err != nil {
169169
return err
170170
}
171171

172-
logger.Tracef("[%s/%s] ==> %s", analysis.ExternalType, analysis.ExternalID, analysis)
172+
logger.Tracef("[%s/%s] ==> %s", analysis.ConfigType, analysis.ExternalID, analysis)
173173
analysis.ConfigID = uuid.MustParse(ci.ID)
174174
analysis.ID = uuid.MustParse(ulid.MustNew().AsUUID())
175175

fixtures/expected/file-git.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"last_modified": "0001-01-01T00:00:00Z",
33
"source": "github.com/flanksource/canary-checker/fixtures/minimal/http_pass_single.yaml",
44
"id": "http-pass-single",
5-
"type": "Canary",
5+
"config_class": "Canary",
66
"config": {
77
"apiVersion": "canaries.flanksource.com/v1",
88
"kind": "Canary",

0 commit comments

Comments
 (0)