diff --git a/src/libs/mysqlHandler.go b/src/libs/mysqlHandler.go index f373af65..79e96543 100644 --- a/src/libs/mysqlHandler.go +++ b/src/libs/mysqlHandler.go @@ -299,7 +299,7 @@ func GetSystemPoliciesFromMySQL(cfg types.ConfigDB, namespace, status string) ([ var results *sql.Rows var err error - query := "SELECT apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime FROM " + TableSystemPolicy_TableName + query := "SELECT apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime,updatedTime,latest FROM " + TableSystemPolicy_TableName if namespace != "" && status != "" { query = query + " WHERE namespace = ? and status = ? " @@ -339,6 +339,8 @@ func GetSystemPoliciesFromMySQL(cfg types.ConfigDB, namespace, status string) ([ &policy.Outdated, &specByte, &policy.GeneratedTime, + &policy.UpdatedTime, + &policy.Latest, ); err != nil { return nil, err } @@ -364,7 +366,7 @@ func GetSystemPoliciesFromMySQL(cfg types.ConfigDB, namespace, status string) ([ } func insertSystemPolicy(cfg types.ConfigDB, db *sql.DB, policy types.KnoxSystemPolicy) error { - stmt, err := db.Prepare("INSERT INTO " + TableSystemPolicy_TableName + "(apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime) values(?,?,?,?,?,?,?,?,?,?)") + stmt, err := db.Prepare("INSERT INTO " + TableSystemPolicy_TableName + "(apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime,updatedTime,latest) values(?,?,?,?,?,?,?,?,?,?,?,?)") if err != nil { return err } @@ -386,7 +388,9 @@ func insertSystemPolicy(cfg types.ConfigDB, db *sql.DB, policy types.KnoxSystemP policy.Metadata["status"], policy.Outdated, spec, - policy.GeneratedTime) + ConvertStrToUnixTime("now"), + ConvertStrToUnixTime("now"), + true) if err != nil { return err } @@ -413,7 +417,7 @@ func UpdateSystemPolicyToMySQL(cfg types.ConfigDB, policy types.KnoxSystemPolicy // set status -> outdated stmt, err := db.Prepare("UPDATE " + TableSystemPolicy_TableName + - " SET apiVersion=?,kind=?,clusterName=?,namespace=?,type=?,status=?,outdated=?,spec=?,generatedTime=? WHERE name = ?") + " SET apiVersion=?,kind=?,clusterName=?,namespace=?,type=?,status=?,outdated=?,spec=?,updatedTime=?,latest=? WHERE name = ?") if err != nil { return err } @@ -434,7 +438,8 @@ func UpdateSystemPolicyToMySQL(cfg types.ConfigDB, policy types.KnoxSystemPolicy policy.Metadata["status"], policy.Outdated, spec, - policy.GeneratedTime, + ConvertStrToUnixTime("now"), + true, policy.Metadata["name"]) if err != nil { return err @@ -530,7 +535,9 @@ func CreateTableSystemPolicyMySQL(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," + + " `latest` BOOLEAN," + " PRIMARY KEY (`id`)" + " );" diff --git a/src/libs/sqliteHandler.go b/src/libs/sqliteHandler.go index 37c6f77f..5d9f29ab 100644 --- a/src/libs/sqliteHandler.go +++ b/src/libs/sqliteHandler.go @@ -289,7 +289,7 @@ func GetSystemPoliciesFromSQLite(cfg types.ConfigDB, namespace, status string) ( var results *sql.Rows var err error - query := "SELECT apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime FROM " + TableSystemPolicySQLite_TableName + query := "SELECT apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime,updatedTime,latest FROM " + TableSystemPolicySQLite_TableName if namespace != "" && status != "" { query = query + " WHERE namespace = ? and status = ? " @@ -329,6 +329,8 @@ func GetSystemPoliciesFromSQLite(cfg types.ConfigDB, namespace, status string) ( &policy.Outdated, &specByte, &policy.GeneratedTime, + &policy.UpdatedTime, + &policy.Latest, ); err != nil { return nil, err } @@ -354,7 +356,7 @@ func GetSystemPoliciesFromSQLite(cfg types.ConfigDB, namespace, status string) ( } func insertSystemPolicySQLite(cfg types.ConfigDB, db *sql.DB, policy types.KnoxSystemPolicy) error { - stmt, err := db.Prepare("INSERT INTO " + TableSystemPolicySQLite_TableName + "(apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime) values(?,?,?,?,?,?,?,?,?,?)") + stmt, err := db.Prepare("INSERT INTO " + TableSystemPolicySQLite_TableName + "(apiVersion,kind,name,clusterName,namespace,type,status,outdated,spec,generatedTime,updatedTime,latest) values(?,?,?,?,?,?,?,?,?,?,?,?)") if err != nil { return err } @@ -376,7 +378,9 @@ func insertSystemPolicySQLite(cfg types.ConfigDB, db *sql.DB, policy types.KnoxS policy.Metadata["status"], policy.Outdated, spec, - policy.GeneratedTime) + ConvertStrToUnixTime("now"), + ConvertStrToUnixTime("now"), + true) if err != nil { return err } @@ -403,7 +407,7 @@ func UpdateSystemPolicyToSQLite(cfg types.ConfigDB, policy types.KnoxSystemPolic // set status -> outdated stmt, err := db.Prepare("UPDATE " + TableSystemPolicySQLite_TableName + - " SET apiVersion=?,kind=?,clusterName=?,namespace=?,type=?,status=?,outdated=?,spec=?,generatedTime=? WHERE name = ?") + " SET apiVersion=?,kind=?,clusterName=?,namespace=?,type=?,status=?,outdated=?,spec=?,updatedTime=?,latest=? WHERE name = ?") if err != nil { return err } @@ -424,7 +428,8 @@ func UpdateSystemPolicyToSQLite(cfg types.ConfigDB, policy types.KnoxSystemPolic policy.Metadata["status"], policy.Outdated, spec, - policy.GeneratedTime, + ConvertStrToUnixTime("now"), + true, policy.Metadata["name"]) if err != nil { return err @@ -508,7 +513,9 @@ func CreateTableSystemPolicySQLite(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," + + " `latest` BOOLEAN," + " PRIMARY KEY (`id`)" + " );" diff --git a/src/types/policyData.go b/src/types/policyData.go index b10b59c2..ac75489f 100644 --- a/src/types/policyData.go +++ b/src/types/policyData.go @@ -253,6 +253,8 @@ type KnoxSystemPolicy struct { Spec KnoxSystemSpec `json:"spec,omitempty" yaml:"spec,omitempty" bson:"spec,omitempty"` GeneratedTime int64 `json:"generatedTime,omitempty" yaml:"generatedTime,omitempty" bson:"generatedTime,omitempty"` + UpdatedTime int64 `json:"updatedTime,omitempty" yaml:"updatedTime,omitempty" bson:"updatedTime,omitempty"` + Latest int64 `json:"latest,omitempty" yaml:"latest,omitempty" bson:"latest,omitempty"` } // ============================= //