Add makefile and mkconfig.c

This commit is contained in:
2023-01-31 11:40:37 -05:00
parent fae60ac762
commit eacca51cb6
4 changed files with 116 additions and 0 deletions

3
.clang-format Executable file
View File

@@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 8
UseTab: ForIndentation

2
.gitignore vendored
View File

@@ -52,3 +52,5 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
.ccls-cache/
bin/

15
Makefile Normal file
View File

@@ -0,0 +1,15 @@
SOURCE_FILES = ./mkconfig.c
OUTPUT=mkconfig
build: $(SOURCE_FILES)
mkdir -p bin/ && \
gcc -Wall $(CFLAGS) $(LIBS) $(SOURCE_FILES) -o bin/$(OUTPUT)
run:
bin/$(OUTPUT)
compile_db: Makefile
bear -- make
clean:
rm bin/$(OUTPUT)

96
mkconfig.c Normal file
View File

@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc < 6) {
fprintf(stderr,
"Usage:\t %s sourceip sourceport desinationip "
"destinationport protocol [-o outputfile]\n",
argv[0]);
fprintf(stderr,
"\tor %s sourcepod sourceport desinationpod "
"destinationport protocol [-o outputfile]\n",
argv[0]);
fprintf(stderr,
"\nExample:\t%s nginx 80 access 5978 TCP "
"-o test.yaml\n",
argv[0]);
fprintf(stderr, "\t\t%s nginx 80 access 5978 TCP\n", argv[0]);
return EX_USAGE;
}
int opt;
char output[200] = "";
while ((opt = getopt(argc, argv, "o:")) != -1) {
switch (opt) {
case 'o':
strcpy(output, optarg);
break;
}
}
if (strlen(output) == 0) {
strcpy(output, "ingress-egress-nginx.yaml");
}
char *sourceaddr = argv[optind];
int sourceport;
sscanf(argv[optind + 1], "%d", &sourceport);
char *destaddr = argv[optind + 2];
int destport;
sscanf(argv[optind + 3], "%d", &destport);
char *protocol = argv[optind + 4];
// printf(
// "Source address: %s\nSource port: %d\nDestination address: "
// "%s\nDestination port: %d\nProtocol: %s\nOutput: %s\n",
// sourceaddr, sourceport, destaddr, destport, protocol, output);
const char *template =
"apiVersion: networking.k8s.io/v1\n"
"kind: NetworkPolicy\n"
"metadata:\n"
" name: test-network-policy\n"
" namespace: policy-demo\n"
"spec:\n"
" podSelector:\n"
" matchLabels:\n"
" run: %s\n"
" ingress:\n"
" - from:\n"
" - ipBlock:\n"
" cidr: 172.17.0.0/16\n"
" - podSelector:\n"
" matchLabels:\n"
" run: %s\n"
" ports:\n"
" - protocol: %s\n"
" port: %d\n"
" egress:\n"
" - to:\n"
" - ipBlock:\n"
" cidr: 10.0.0.0/24\n"
" ports:\n"
" - protocol: %s\n"
" port: %d\n";
// printf("----------TEMPLATE----------\n");
// printf(template, sourceaddr, destaddr, protocol, sourceport,
// protocol, destport); printf("\n----------------------------\n");
FILE *outputFile = fopen(output, "w");
fprintf(outputFile, template, sourceaddr, destaddr, protocol,
sourceport, protocol, destport);
fclose(outputFile);
char cmd[100];
sprintf(cmd, "kubectl apply -f ./%s", output);
printf("%s\n", cmd);
system(cmd);
return 0;
}