K8s/kubectl resource object - kubernetes

My interest is in exploring how K8s resource objects are created from specs submitted by client-API; and how they are communicated across kube master services and pods.
I have started exploring the K8s source code and reached certain depth.
For this exploration I am using kubectl create -f <path to yaml> and tracking how this command is handled in source code.
here is brief summary
https://github.com/kubernetes/kubernetes/blob/v1.14.0/pkg/kubectl/cmd/create/create.go#L96 I started with this function and followed on function trail (thanks to this blog); and gradually reached to
https://github.com/kubernetes/kubernetes/blob/v1.14.0/staging/src/k8s.io/client-go/rest/request.go#L708 - http request is formed here
https://github.com/kubernetes/kubernetes/blob/v1.14.0/staging/src/k8s.io/client-go/rest/request.go#L395 - the body of the request is formed here.
https://github.com/kubernetes/kubernetes/blob/v1.14.0/staging/src/k8s.io/client-go/rest/request.go#L732 - this functions send http request and gets response back for processing, here I have addedklog.Infof("custom log sizeofdata:%T$$$$,%d$$$$,%s$$$$,%#v",*req,unsafe.Sizeof(*req),*req,*req) to find size of the request being sent.
My hypothesis is; this request size in bytes should change as the change in specs submitted by the user (yaml file passed with kubectl create -f)
Now I tried to feed two different resource spec(with kubectl create -f ) and see how the HTTP request and request body changes (its size).
Yaml 1 - nginx sample
https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/deployment.yaml
Yaml 2 - centos sample
apiVersion: v1
kind: Pod
metadata:
name: cent-os-test
spec:
containers:
- name: centos-test-container
image: centos
command: ["sleep"]
args: ["100"]
restartPolicy: Never
Surprisingly both times output of the logging line above was same Byte size (248 Bytes)
for log requested by klog.Infof("custom log sizeofdata:%T$$$$,%d$$$$,%s$$$$,%#v",*req,unsafe.Sizeof(*req),*req,*req)
I have used $$$$ as field separator while printing.
Following are log entries in case of both of yamls
for Yaml 2 centos ---
I0405 22:22:37.285666 360723 request.go:745] custom_log size of data : http.Request $$$$, 248 $$$$, {POST https://10.xxx.xxx.xxx:6443/api/v1/namespaces/default/pods HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[application/json] Content-Type:[application/json]] {%!s(*bytes.Reader=&{[123 34 97 112 105 86 101 114 115 105 111 110 34 58 34 118 49 34 44 34 107 105 110 100 34 58 34 80 111 100 34 44 34 109 101 116 97 100 97 116 97 34 58 123 34 110 97 109 101 34 58 34 99 101 110 116 45 111 115 45 116 101 115 116 34 44 34 110 97 109 101 115 112 97 99 101 34 58 34 100 101 102 97 117 108 116 34 125 44 34 115 112 101 99 34 58 123 34 99 111 110 116 97 105 110 101 114 115 34 58 91 123 34 97 114 103 115 34 58 91 34 49 48 48 34 93 44 34 99 111 109 109 97 110 100 34 58 91 34 115 108 101 101 112 34 93 44 34 105 109 97 103 101 34 58 34 99 101 110 116 111 115 34 44 34 110 97 109 101 34 58 34 99 101 110 116 111 115 45 116 101 115 116 45 99 111 110 116 97 105 110 101 114 34 125 93 44 34 114 101 115 116 97 114 116 80 111 108 105 99 121 34 58 34 78 101 118 101 114 34 125 125 10] 0 -1})} %!s(func() (io.ReadCloser, error)=0x8087b0) %!s(int64=223) [] %!s(bool=false) 10.xxx.xxx.xxx:6443 map[] map[] %!s(*multipart.Form=<nil>) map[] %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) <nil>} $$$$, http.Request{Method:"POST", URL:(*url.URL)(0xc001dc6880), Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Accept":[]string{"application/json"}, "Content-Type":[]string{"application/json"}}, Body:ioutil.nopCloser{Reader:(*bytes.Reader)(0xc000886f60)}, GetBody:(func() (io.ReadCloser, error))(0x8087b0), ContentLength:223, TransferEncoding:[]string(nil), Close:false, Host:"10.xxx.xxx.xxx:6443", Form:url.Values(nil), PostForm:url.Values(nil), MultipartForm:(*multipart.Form)(nil), Trailer:http.Header(nil), RemoteAddr:"", RequestURI:"", TLS:(*tls.ConnectionState)(nil), Cancel:(<-chan struct {})(nil), Response:(*http.Response)(nil), ctx:context.Context(nil)}
for Yaml 1 nginx ---
I0406 00:07:19.873854 410863 request.go:745] custom_log size of data : http.Request $$$$, 248 $$$$, {POST https://10.xxx.xxx.xxx:6443/apis/apps/v1/namespaces/default/deployments HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[application/json] Content-Type:[application/json]] {%!s(*bytes.Reader=&{[123 34 97 112 105 86 101 114 115 105 111 110 34 58 34 97 112 112 115 47 118 49 34 44 34 107 105 110 100 34 58 34 68 101 112 108 111 121 109 101 110 116 34 44 34 109 101 116 97 100 97 116 97 34 58 123 34 110 97 109 101 34 58 34 110 103 105 110 120 45 100 101 112 108 111 121 109 101 110 116 34 44 34 110 97 109 101 115 112 97 99 101 34 58 34 100 101 102 97 117 108 116 34 125 44 34 115 112 101 99 34 58 123 34 114 101 112 108 105 99 97 115 34 58 50 44 34 115 101 108 101 99 116 111 114 34 58 123 34 109 97 116 99 104 76 97 98 101 108 115 34 58 123 34 97 112 112 34 58 34 110 103 105 110 120 34 125 125 44 34 116 101 109 112 108 97 116 101 34 58 123 34 109 101 116 97 100 97 116 97 34 58 123 34 108 97 98 101 108 115 34 58 123 34 97 112 112 34 58 34 110 103 105 110 120 34 125 125 44 34 115 112 101 99 34 58 123 34 99 111 110 116 97 105 110 101 114 115 34 58 91 123 34 105 109 97 103 101 34 58 34 110 103 105 110 120 58 49 46 55 46 57 34 44 34 110 97 109 101 34 58 34 110 103 105 110 120 34 44 34 112 111 114 116 115 34 58 91 123 34 99 111 110 116 97 105 110 101 114 80 111 114 116 34 58 56 48 125 93 125 93 125 125 125 125 10] 0 -1})} %!s(func() (io.ReadCloser, error)=0x8087b0) %!s(int64=316) [] %!s(bool=false) 10.xxx.xxx.xxx:6443 map[] map[] %!s(*multipart.Form=<nil>) map[] %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) <nil>} $$$$, http.Request{Method:"POST", URL:(*url.URL)(0xc000a96380), Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Accept":[]string{"application/json"}, "Content-Type":[]string{"application/json"}}, Body:ioutil.nopCloser{Reader:(*bytes.Reader)(0xc0008abb90)}, GetBody:(func() (io.ReadCloser, error))(0x8087b0), ContentLength:316, TransferEncoding:[]string(nil), Close:false, Host:"10.xxx.xxx.xxx:6443", Form:url.Values(nil), PostForm:url.Values(nil), MultipartForm:(*multipart.Form)(nil), Trailer:http.Header(nil), RemoteAddr:"", RequestURI:"", TLS:(*tls.ConnectionState)(nil), Cancel:(<-chan struct {})(nil), Response:(*http.Response)(nil), ctx:context.Context(nil)}
Why both of times the HTTP request size is 248 bytes? Shouldn’t it increase in case of the nginx yaml since it passes more lines of spec information?

Would you refer to the length of the *req variable rather then it's size
klog.Infof("custom log sizeofdata:%T$$$$,%d$$$$,%s$$$$,%#v",*req,len(*req),*req,*req)
Similar to the below snippet illustrating that size of a variable of type string is always 8 but the length of a string that a variable references can vary:
package main
import "fmt"
import "unsafe"
func main() {
s1 := "foo"
s2 := "foobar"
fmt.Printf("s1 size: %T, %d\n", s1, unsafe.Sizeof(s1))
fmt.Printf("s2 size: %T, %d\n", s2, unsafe.Sizeof(s2))
fmt.Printf("s1 len: %T, %d\n", s1, len(s1))
fmt.Printf("s2 len: %T, %d\n", s2, len(s2))
}
Result:
s1 size: string, 8
s2 size: string, 8
s1 len: string, 3
s2 len: string, 6

Related

Mongoexport troubleshooting

I am new to MongoDB. I am trying to save a query result to a json file however keep getting this error:
query '[102 105 110 100 40 123 32 36 97 110 100 58 91 32 123 100 105 115 112 108 97 121 78 97 109 101 58 47 94 65 47 105 125 44 32 123 100 105 115 112 108 97 121 78 97 109 101 58 47 101 115 36 47 125 32 44 123 102 114 105 101 110 100 115 67 111 117 110 116 58 123 36 108 116 58 50 53 125 125 93 32 125 44 123 100 105 115 112 108 97 121 78 97 109 101 58 49 44 102 111 108 108 111 119 101 114 115 67 111 117 110 116 58 49 44 102 114 105 101 110 100 115 67 111 117 110 116 58 49 125 41 46 115 111 114 116 40 123 100 105 115 112 108 97 121 78 97 109 101 58 45 49 125 41]' is not valid JSON: invalid character 'i' in literal false (expecting 'a') try 'mongoexport --help' for more information
The code I am using is
mongoexport --db cw2 --collection cl --query="find({ $and:[ {displayName:/^A/i}, {displayName:/es$/} ,{friendsCount:{$lt:25}}] },{"displayName":1,"followersCount":1,"friendsCount":1}).sort({"displayName":-1})" --out=data/cw2q2.json
Can anyone see what the issue is? I am not sure i understand what the actual message is saying.

how to export mongodb query to csv

not able to execute below query on linux server with mongoDb
mongoexport --db MongoLog --collection dCDMongoLog --query '{$and:[{'jsonObject.responseRaw':{$regex:'<Amount>1000'}},{'jsonObject.createDate':{$gt:'2021-04-01'}},{'jsonObject.createDate':{$lte:'2021-04-08'}}]}' --csv -f jsonObject.mobile,jsonObject.createDate --out /opt/data1234.csv
getting below error
-bash: Amount: No such file or directory
even tried this way :
mongoexport -d MongoLog -c dCDMongoLog -q "{$and:[{'jsonObject.responseRaw':{$regex:'<Amount>1000'}},{'jsonObject.createDate':{$gt:'2021-04-01'}},{'jsonObject.createDate':{$lte:'2021-04-08'}}]}" -f jsonObject.mobile,jsonObject.createDate --type=csv > /opt/data1234.csv
but getting this error:
error validating settings: query '[123 58 91 123 39 106 115 111 110 79 98 106 101 99 116 46 114 101 115 112 111 110 115 101 82 97 119 39 58 123 58 39 60 65 109 111 117 110 116 62 49 48 48 48 39 125 125 44 123 39 106 115 111 110 79 98 106 101 99 116 46 99 114 101 97 116 101 68 97 116 101 39 58 123 58 39 50 48 50 49 45 48 52 45 48 49 39 125 125 44 123 39 106 115 111 110 79 98 106 101 99 116 46 99 114 101 97 116 101 68 97 116 101 39 58 123 58 39 50 48 50 49 45 48 52 45 48 56 39 125 125 93 125]' is not valid JSON: invalid character ':' looking for beginning of object key string
2021-04-16T20:01:00.729-0400 try 'mongoexport --help' for more information
nothing seems to be running. Any other ways?
mongoexport query sucks! it requires single quotes outside and double quotes inside.
below command worked for me.
mongoexport -d MongoLog -c dCDMongoLog -q '{"$and":[{"jsonObject.responseRaw":{$regex:"<Amount>1000"}},{"jsonObject.createDate":{$gt:"2021-04-01"}},{"jsonObject.createDate":{$lte:"2021-04-08"}}]}' -f jsonObject.mobile,jsonObject.createDate --type=csv > /opt/data1235.csv

Getting error regarding --query in mongoexport

I am getting an error while writing the mongoExport code in node.js
Command:
var quer_var= `'{"_id.userId":"${userId}","_id.type":"gapAnalysis","_id.timeStamp":"${timeStamp}"}'`
var command = `mongoexport --db ${process.env.DB_NAME} --collection preCaches --query ${quer_var} --type csv --fields MPN,L4Category,RevenueImpact --out ${process.env.DOWNLOAD_DIR_PATH+fileName}`
Error:
'{"_id.userId":"5fbf7fd344c67c48085383b1","_id.type":"gapAnalysis","_id.timeStamp":"2020-12-31T14:30:22+05:30"}'
2020-12-31T14:30:33.019+0530 query '[39 123 95 105 100 46 117 115 101 114 73 100 58 53 102 98 102 55 102 100 51 52 52 99 54 55 99 52 56 48 56 53 51 56 51 98 49 44 95 105 100 46 116 121 112 101 58 103 97 112 65 110 97 108 121 115 105 115 44 95 105 100 46 116 105 109 101 83 116 97 109 112 58 50 48 50 48 45 49 50 45 51 49 84 49 52 58 51 48 58 50 50 43 48 53 58 51 48 125 39]'
is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}
2020-12-31T14:30:33.022+0530 try 'mongoexport --help' for more information
please help me to solve this.

MATLAB Serial incorrectly sending values

I am trying to communicate with a StepRocker motor controller/drive using MATLAB. I have figured out how to format their commands and have been able to get it to move to many of commands. Their commands require that the values be sent in a binary format over RS-232. I started running into some issues and started using an Arduino to echo my commands.
I am currently sending the values 1 - 255 just to test sending bytes over the serial channel. I am sending it to an Arduino and reading the bytes back out of the Arduino. All numbers work except for the numbers between 128 and 159. This is problematic since some of my commands require values in this range.
MATLAB Script:
obj = instrfind; delete(obj); % cleanup any lost objects
clear; close force all; clc; % clear the workspace
sM = serial('COM2','BaudRate',9600, 'Terminator', 'CR', 'Timeout', 10);
fopen(sM);
pause(2); % give port time to open
% make list of numbers 1-255, skip 10/13 since they terminate on the
% arduino
cmd = [1:9 11:12 14:255];
% Send command and get echo
fprintf(sM,cmd);
pause(0.5); % give time for echo
echo = fscanf(sM);
uint8(echo)
fclose(sM);
After executing this script I get the following output:
ans =
Columns 1 through 22
1 2 3 4 5 6 7 8 9 11 12 14 15 16 17 18 19 20 21 22 23 24
Columns 23 through 44
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Columns 45 through 66
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
Columns 67 through 88
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
Columns 89 through 110
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
Columns 111 through 132
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 63 63 63 63 63 63 63
Columns 133 through 154
63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
Columns 155 through 176
63 63 63 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
Columns 177 through 198
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
Columns 199 through 220
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
Columns 221 through 242
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
Columns 243 through 254
245 246 247 248 249 250 251 252 253 254 255 13
Looking at the output, you can see that the numbers between 128 and 159 all come through as 63. I am at a loss for what is causing this issue. This is very repeatable, and the problem occurs anytime there is a number in this range, no matter its position in the string, no matter the length of the serial string.
Arduino Code:
bool stringComplete = false;
String inputString = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (stringComplete) {
Serial.print(inputString);
stringComplete = false;
inputString = "";
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\r') {
stringComplete = true;
}
}
}
Any suggestions or ideas on how to solve this problem?

How can I display a large matrix without the word "Columns" appearing?

I want to display a large matrix, but I don't like the words "Columns x to y" to show. How can I do this?
You can use the function NUM2STR to format a large 2-D matrix A into a character array and display that. For example:
>> A = magic(15); %# This would likely break up columns when displayed
>> num2str(A) %# This won't
ans =
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120
138 155 172 189 206 223 15 17 34 51 68 85 102 119 121
154 171 188 205 222 14 16 33 50 67 84 101 118 135 137
170 187 204 221 13 30 32 49 66 83 100 117 134 136 153
186 203 220 12 29 31 48 65 82 99 116 133 150 152 169
202 219 11 28 45 47 64 81 98 115 132 149 151 168 185
218 10 27 44 46 63 80 97 114 131 148 165 167 184 201
9 26 43 60 62 79 96 113 130 147 164 166 183 200 217
25 42 59 61 78 95 112 129 146 163 180 182 199 216 8
41 58 75 77 94 111 128 145 162 179 181 198 215 7 24
57 74 76 93 110 127 144 161 178 195 197 214 6 23 40
73 90 92 109 126 143 160 177 194 196 213 5 22 39 56
89 91 108 125 142 159 176 193 210 212 4 21 38 55 72
105 107 124 141 158 175 192 209 211 3 20 37 54 71 88
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104