Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code to aggregate network policy per deployment/endpoint #499

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/libs/dbHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func UpdateOutdatedNetworkPolicy(cfg types.ConfigDB, outdatedPolicy string, late
}
}

func UpdateNetworkPolicy(cfg types.ConfigDB, policy types.KnoxNetworkPolicy) {
if cfg.DBDriver == "mysql" {
if err := UpdateNetworkPolicyToMySQL(cfg, policy); err != nil {
log.Error().Msg(err.Error())
}
} else if cfg.DBDriver == "sqlite3" {
if err := UpdateNetworkPolicyToSQLite(cfg, policy); err != nil {
log.Error().Msg(err.Error())
}
}
}

func InsertNetworkPolicies(cfg types.ConfigDB, policies []types.KnoxNetworkPolicy) {
if cfg.DBDriver == "mysql" {
if err := InsertNetworkPoliciesToMySQL(cfg, policies); err != nil {
Expand Down
111 changes: 57 additions & 54 deletions src/libs/dbHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func TestGetNetworkPolicies(t *testing.T) {
"status", // str
"outdated", // str
"spec", // []byte
"generatedTime", // int
"generatedTime", // uint64
"updatedTime", // uint64
}).
AddRow("", "test", flowID, "", "", "", "", "", "", "", spec, 0)
AddRow("", "test", flowID, "", "", "", "", "", "", "", spec, 0, 0)

mock.ExpectQuery("^SELECT (.+) FROM network_policy*").
WillReturnRows(rows)
Expand Down Expand Up @@ -71,18 +72,19 @@ func TestInsertNetworkPolicies(t *testing.T) {
prep := mock.ExpectPrepare("INSERT INTO network_policy")
prep.ExpectExec().
WithArgs(
"", // str
"kind", // str
flowID, // []byte
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
spec, // []byte
0, // int
"", // str
"kind", // str
flowID, // []byte
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
spec, // []byte
sqlmock.AnyArg(), // uint64
sqlmock.AnyArg(), // uint64
).WillReturnResult(sqlmock.NewResult(0, 1))

nfe := []types.KnoxNetworkPolicy{
Expand All @@ -100,44 +102,45 @@ func TestInsertNetworkPolicies(t *testing.T) {
}

func TestInsertNetworkPoliciesSQLite(t *testing.T) {
// prepare mock sqlite
_, mock := NewMock()

policy := types.KnoxNetworkPolicy{}

specPtr := &policy.Spec
spec, _ := json.Marshal(specPtr)

flowIDsPrt := &policy.FlowIDs
flowID, _ := json.Marshal(flowIDsPrt)

prep := mock.ExpectPrepare("INSERT INTO network_policy")
prep.ExpectExec().
WithArgs(
"", // str
"kind", // str
flowID, // []byte
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
spec, // []byte
0, // int
).WillReturnResult(sqlmock.NewResult(0, 1))

nfe := []types.KnoxNetworkPolicy{
types.KnoxNetworkPolicy{
Kind: "kind",
},
}

err := InsertNetworkPoliciesToSQLite(types.ConfigDB{DBDriver: "sqlite3"}, nfe)
assert.NoError(t, err)

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf(Unmet+"%s", err)
}
// prepare mock sqlite
_, mock := NewMock()

policy := types.KnoxNetworkPolicy{}

specPtr := &policy.Spec
spec, _ := json.Marshal(specPtr)

flowIDsPrt := &policy.FlowIDs
flowID, _ := json.Marshal(flowIDsPrt)

prep := mock.ExpectPrepare("INSERT INTO network_policy")
prep.ExpectExec().
WithArgs(
"", // str
"kind", // str
flowID, // []byte
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
"", // str
spec, // []byte
sqlmock.AnyArg(), // uint64
sqlmock.AnyArg(), // uint64
).WillReturnResult(sqlmock.NewResult(0, 1))

nfe := []types.KnoxNetworkPolicy{
types.KnoxNetworkPolicy{
Kind: "kind",
},
}

err := InsertNetworkPoliciesToSQLite(types.ConfigDB{DBDriver: "sqlite3"}, nfe)
assert.NoError(t, err)

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf(Unmet+"%s", err)
}
}
49 changes: 45 additions & 4 deletions src/libs/mysqlHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetNetworkPoliciesFromMySQL(cfg types.ConfigDB, cluster, namespace, status,
var results *sql.Rows
var err error

query := "SELECT apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime FROM " + TableNetworkPolicy_TableName
query := "SELECT apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime,updatedTime FROM " + TableNetworkPolicy_TableName

var whereClause string
var args []interface{}
Expand Down Expand Up @@ -140,6 +140,7 @@ func GetNetworkPoliciesFromMySQL(cfg types.ConfigDB, cluster, namespace, status,
&policy.Outdated,
&specByte,
&policy.GeneratedTime,
&policy.UpdatedTime,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -170,6 +171,42 @@ func GetNetworkPoliciesFromMySQL(cfg types.ConfigDB, cluster, namespace, status,
return policies, nil
}

func UpdateNetworkPolicyToMySQL(cfg types.ConfigDB, policy types.KnoxNetworkPolicy) error {
db := connectMySQL(cfg)
defer db.Close()

// set status -> outdated
stmt, err := db.Prepare("UPDATE " + TableNetworkPolicy_TableName +
" SET apiVersion=?,kind=?,cluster_name=?,namespace=?,type=?,status=?,outdated=?,spec=?,updatedTime=? WHERE name = ?")
if err != nil {
return err
}
defer stmt.Close()

specPointer := &policy.Spec
spec, err := json.Marshal(specPointer)
if err != nil {
return err
}

_, err = stmt.Exec(
policy.APIVersion,
policy.Kind,
policy.Metadata["cluster_name"],
policy.Metadata["namespace"],
policy.Metadata["type"],
policy.Metadata["status"],
policy.Outdated,
spec,
ConvertStrToUnixTime("now"),
policy.Metadata["name"])
if err != nil {
return err
}

return nil
}

func UpdateOutdatedNetworkPolicyFromMySQL(cfg types.ConfigDB, outdatedPolicy string, latestPolicy string) error {
db := connectMySQL(cfg)
defer db.Close()
Expand Down Expand Up @@ -204,7 +241,7 @@ func UpdateOutdatedNetworkPolicyFromMySQL(cfg types.ConfigDB, outdatedPolicy str
}

func insertNetworkPolicy(cfg types.ConfigDB, db *sql.DB, policy types.KnoxNetworkPolicy) error {
stmt, err := db.Prepare("INSERT INTO " + TableNetworkPolicy_TableName + "(apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime) values(?,?,?,?,?,?,?,?,?,?,?,?)")
stmt, err := db.Prepare("INSERT INTO " + TableNetworkPolicy_TableName + "(apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime,updatedTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
}
Expand All @@ -222,6 +259,8 @@ func insertNetworkPolicy(cfg types.ConfigDB, db *sql.DB, policy types.KnoxNetwor
return err
}

currTime := ConvertStrToUnixTime("now")

_, err = stmt.Exec(policy.APIVersion,
policy.Kind,
flowids,
Expand All @@ -233,7 +272,8 @@ func insertNetworkPolicy(cfg types.ConfigDB, db *sql.DB, policy types.KnoxNetwor
policy.Metadata["status"],
policy.Outdated,
spec,
policy.GeneratedTime)
currTime,
currTime)
if err != nil {
return err
}
Expand Down Expand Up @@ -506,7 +546,8 @@ func CreateTableNetworkPolicyMySQL(cfg types.ConfigDB) error {
" `status` varchar(10) DEFAULT NULL," +
" `outdated` varchar(50) DEFAULT NULL," +
" `spec` JSON DEFAULT NULL," +
" `generatedTime` int DEFAULT NULL," +
" `generatedTime` bigint NOT NULL," +
" `updatedTime` bigint NOT NULL," +
" PRIMARY KEY (`id`)" +
" );"

Expand Down
48 changes: 44 additions & 4 deletions src/libs/sqliteHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetNetworkPoliciesFromSQLite(cfg types.ConfigDB, cluster, namespace, status
var results *sql.Rows
var err error

query := "SELECT apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime FROM " + TableNetworkPolicySQLite_TableName
query := "SELECT apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime,updatedTime FROM " + TableNetworkPolicySQLite_TableName
if cluster != "" && namespace != "" && status != "" {
query = query + " WHERE cluster_name = ? and namespace = ? and status = ? "
results, err = db.Query(query, cluster, namespace, status)
Expand Down Expand Up @@ -130,6 +130,7 @@ func GetNetworkPoliciesFromSQLite(cfg types.ConfigDB, cluster, namespace, status
&policy.Outdated,
&specByte,
&policy.GeneratedTime,
&policy.UpdatedTime,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,6 +161,41 @@ func GetNetworkPoliciesFromSQLite(cfg types.ConfigDB, cluster, namespace, status
return policies, nil
}

func UpdateNetworkPolicyToSQLite(cfg types.ConfigDB, policy types.KnoxNetworkPolicy) error {
db := connectSQLite(cfg)
defer db.Close()

stmt, err := db.Prepare("UPDATE " + TableNetworkPolicySQLite_TableName +
" SET apiVersion=?,kind=?,cluster_name=?,namespace=?,type=?,status=?,outdated=?,spec=?,updatedTime=? WHERE name = ?")
if err != nil {
return err
}
defer stmt.Close()

specPointer := &policy.Spec
spec, err := json.Marshal(specPointer)
if err != nil {
return err
}

_, err = stmt.Exec(
policy.APIVersion,
policy.Kind,
policy.Metadata["cluster_name"],
policy.Metadata["namespace"],
policy.Metadata["type"],
policy.Metadata["status"],
policy.Outdated,
spec,
ConvertStrToUnixTime("now"),
policy.Metadata["name"])
if err != nil {
return err
}

return nil
}

func UpdateOutdatedNetworkPolicyFromSQLite(cfg types.ConfigDB, outdatedPolicy string, latestPolicy string) error {
db := connectSQLite(cfg)
defer db.Close()
Expand Down Expand Up @@ -194,7 +230,7 @@ func UpdateOutdatedNetworkPolicyFromSQLite(cfg types.ConfigDB, outdatedPolicy st
}

func insertNetworkPolicySQLite(cfg types.ConfigDB, db *sql.DB, policy types.KnoxNetworkPolicy) error {
stmt, err := db.Prepare("INSERT INTO " + TableNetworkPolicySQLite_TableName + "(apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime) values(?,?,?,?,?,?,?,?,?,?,?,?)")
stmt, err := db.Prepare("INSERT INTO " + TableNetworkPolicySQLite_TableName + "(apiVersion,kind,flow_ids,name,cluster_name,namespace,type,rule,status,outdated,spec,generatedTime,updatedTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
}
Expand All @@ -212,6 +248,8 @@ func insertNetworkPolicySQLite(cfg types.ConfigDB, db *sql.DB, policy types.Knox
return err
}

currTime := ConvertStrToUnixTime("now")

_, err = stmt.Exec(policy.APIVersion,
policy.Kind,
flowids,
Expand All @@ -223,7 +261,8 @@ func insertNetworkPolicySQLite(cfg types.ConfigDB, db *sql.DB, policy types.Knox
policy.Metadata["status"],
policy.Outdated,
spec,
policy.GeneratedTime)
currTime,
currTime)
if err != nil {
return err
}
Expand Down Expand Up @@ -484,7 +523,8 @@ func CreateTableNetworkPolicySQLite(cfg types.ConfigDB) error {
" `status` varchar(10) DEFAULT NULL," +
" `outdated` varchar(50) DEFAULT NULL," +
" `spec` JSON DEFAULT NULL," +
" `generatedTime` int DEFAULT NULL," +
" `generatedTime` bigint NOT NULL," +
" `updatedTime` bigint NOT NULL," +
" PRIMARY KEY (`id`)" +
" );"

Expand Down
Loading