diff --git a/.clang-format b/.clang-format new file mode 100755 index 0000000..4dc32f5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Google +IndentWidth: 8 +UseTab: ForIndentation diff --git a/.gitignore b/.gitignore index cd531cf..2a3aeb3 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,5 @@ Module.symvers Mkfile.old dkms.conf +.ccls-cache/ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..77f0aaf --- /dev/null +++ b/Makefile @@ -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) diff --git a/mkconfig.c b/mkconfig.c new file mode 100644 index 0000000..7853c6b --- /dev/null +++ b/mkconfig.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +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; +}