Skip to content

Commit

Permalink
fixed small typos in readme, now parsing in data for the bisect succe…
Browse files Browse the repository at this point in the history
…ssfully, specintervalhistory now implemented so you can do start and end params on the history, and bisect currently is actually having problems.
  • Loading branch information
djarotech committed Aug 16, 2018
1 parent f2c1270 commit 4e58bb4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/pos
5) Find out in which version the user 'pallavi' was given password 'pass123'

```
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/bisect?field1=username&value1=pallavi&field2=password&value2=pass123"
kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/bisect?field1=username&value1=pallavi&field2=password&value2=pass123"
```
![alt text](https://github.com/cloud-ark/kubeprovenance/raw/master/docs/spechistory.png)
![alt text](https://github.com/cloud-ark/kubeprovenance/raw/master/docs/getdiff_databases.png)
Expand Down Expand Up @@ -203,10 +203,10 @@ kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgr
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=2&field=databases"
```

5) Find out in which version the field 'abc' was given value 'def'
5) Find out in which version the user 'pallavi' was given password 'pass123'

```
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/bisect?field=abc&value=def"
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/bisect?field1=username&value1=pallavi&field2=password&value2=pass123"
```

## Troubleshooting tips:
Expand Down
34 changes: 28 additions & 6 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,36 +194,58 @@ func getHistory(request *restful.Request, response *restful.Response) {
provenanceInfo := "Resource Name:" + resourceName + " Resource Kind:" + resourceKind + "\n"
response.Write([]byte(provenanceInfo))
intendedProvObj := provenance.FindProvenanceObjectByName(resourceName, provenance.AllProvenanceObjects)
//optional parameters
start := request.QueryParameter("start")
end := request.QueryParameter("end")

//TODO: Validate request based on the correct namespace and the correct plural type.
//I have the namespace/pluralkind datain my ProvenanceOfObject struct so it is easy to make these changes later
if intendedProvObj == nil {
s := fmt.Sprintf("Could not find any provenance history for resource name: %s", resourceName)
response.Write([]byte(s))
} else {
//TODO: handle optional interval parameters
response.Write([]byte(intendedProvObj.ObjectFullHistory.SpecHistory()))
if start != "" && end != "" { //have both a start and an end
fmt.Printf("Start:%s", start)
fmt.Printf("End:%s", end)
startInt, err := strconv.Atoi(start)
if err != nil {
s := fmt.Sprintf("Could not parse start query parameter to int: %s", err.Error())
response.Write([]byte(s))
return
}
endInt, err := strconv.Atoi(end)
if err != nil {
s := fmt.Sprintf("Could not parse end query parameter to int: %s", err.Error())
response.Write([]byte(s))
return
}
fmt.Printf("Spec history starting with version %d and ending with version %d", startInt, endInt)
response.Write([]byte(intendedProvObj.ObjectFullHistory.SpecHistoryInterval(startInt, endInt)))
} else { //start and end
fmt.Printf("here")
response.Write([]byte(intendedProvObj.ObjectFullHistory.SpecHistory()))
}
}

}

func bisect(request *restful.Request, response *restful.Response) {
fmt.Println("Inside bisect")
fmt.Println("Inside bisectaa")
resourceName := request.PathParameter("resource-id")
requestPath := request.Request.URL.Path
resourcePathSlice := strings.Split(requestPath, "/")
resourceKind := resourcePathSlice[6] // Kind is 7th element in the slice

var provenanceInfo string
provenanceInfo = "Resource Name:" + resourceName + " Resource Kind:" + resourceKind

//TODO: find out how to get entire query string, split on &, and pass in an array of fields/values to the bisect method.
field1 := request.QueryParameter("field1")
value1 := request.QueryParameter("value1")

field2 := request.QueryParameter("field2")
value2 := request.QueryParameter("value2")
provenanceInfo = provenanceInfo + " Field1:" + field1 + " Value1: " + value1 + "\n"
provenanceInfo = provenanceInfo + " Field2:" + field1 + " Value2: " + value1 + "\n"
provenanceInfo = provenanceInfo + " Field1:" + field1 + " Value1: " + value1
provenanceInfo = provenanceInfo + " Field2:" + field2 + " Value2: " + value2 + "\n"

fmt.Printf("ProvenanceInfo:%v", provenanceInfo)

Expand Down
40 changes: 20 additions & 20 deletions pkg/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (o ObjectLineage) Bisect(field1, value1, field2, value2 string) string {
for _, version := range s {
specs = append(specs, o[version]) //cast Spec to String
}
// fmt.Println(specs)
noSpecFound := fmt.Sprintf("Bisect for field %s: %s, %s: %s was not successful. Custom resource never reached this state.", field1, value1, field2, value2)

if len(specs) == 1 {
Expand Down Expand Up @@ -249,22 +250,25 @@ func (o ObjectLineage) Bisect(field1, value1, field2, value2 string) string {

//TODO: add optional parameters to spechistory route in apiserver.go, and call this method.
// Right now it is actually unused
func (o ObjectLineage) SpecHistoryInterval(vNumStart, vNumEnd int) []string {
func (o ObjectLineage) SpecHistoryInterval(vNumStart, vNumEnd int) string {
//order keys so we append in order later, reference: https://blog.golang.org/go-maps-in-action#TOC_7.
s := make([]Spec, 0)
for _, spec := range o {
s = append(s, spec)
s := make([]int, 0)
for _, value := range o {
s = append(s, value.Version)
}
sort.Slice(s, func(i, j int) bool {
return s[i].Version < s[i].Version
})
specs := make([]string, 0)
for _, spec := range s {
sort.Ints(s)
//get all versions, sort by version, make string array of them
specs := make([]Spec, 0)
for _, version := range s {
specs = append(specs, o[version]) //cast Spec to String
}
specStrings := make([]string, 0)
for _, spec := range specs {
if spec.Version >= vNumStart && spec.Version <= vNumEnd {
specs = append(specs, spec.String())
specStrings = append(specStrings, spec.String())
}
}
return specs
return strings.Join(specStrings, "\n")
}

func (o ObjectLineage) FullDiff(vNumStart, vNumEnd int) string {
Expand Down Expand Up @@ -426,7 +430,7 @@ func ParseRequestObject(objectProvenance *ProvenanceOfObject, requestObjBytes []
func buildSpec(spec map[string]interface{}) Spec {
mySpec := *NewSpec()
for attribute, value := range spec {
var isMap, isStringSlice, isString, isInt, isFloat bool
var isMap, isStringSlice, isString, isInt bool
//note that I cannot do type assertions because the underlying data
//of the interface{} is not a map[string]string or an []slice
//so that means that every type assertion to
Expand All @@ -444,10 +448,6 @@ func buildSpec(spec map[string]interface{}) Spec {
if err := json.Unmarshal(bytes, &plainStringField); err == nil {
isString = true
}
var floatField float64
if err := json.Unmarshal(bytes, &floatField); err == nil {
isFloat = true
}
var intField int
if err := json.Unmarshal(bytes, &intField); err == nil {
isInt = true
Expand All @@ -460,7 +460,7 @@ func buildSpec(spec map[string]interface{}) Spec {
//passwords = [22d732, 4343e2, 434343b]
attributeToSlices := make(map[string][]string, 0)
//build this and then i will loop through and add this to the spec
for _, mapl := range mapSliceField {
for _, mapl := range mapSliceField { //this is an []map[string]string

for key, data := range mapl {
slice, ok := attributeToSlices[key]
Expand All @@ -469,11 +469,13 @@ func buildSpec(spec map[string]interface{}) Spec {
attributeToSlices[key] = slice
} else { // first time seeing this key
slice := make([]string, 0)
slice = append(slice, data)
attributeToSlices[key] = slice
}
}
}
//now add to the attributes

//now add to the spec attributes
for key, value := range attributeToSlices {
mySpec.AttributeToData[key] = value
}
Expand All @@ -482,8 +484,6 @@ func buildSpec(spec map[string]interface{}) Spec {
mySpec.AttributeToData[attribute] = stringSliceField
case isString:
mySpec.AttributeToData[attribute] = plainStringField
case isFloat:
mySpec.AttributeToData[attribute] = floatField
case isInt:
mySpec.AttributeToData[attribute] = intField
default:
Expand Down

0 comments on commit 4e58bb4

Please sign in to comment.